网站首页/ 信息中心/ 行业信息/

综合档案管理系统打印管理全流程实操指南

发布时间:2026年06月17日 03:05:20 浏览量:0

技术选型与核心依赖配置

在综合档案管理系统中,打印功能通常要求高精度、支持套打以及复杂的版式设计。为了实现前端所见即所得且后端可归档的打印方案,本文采用Java后端生成PDF + 前端预览下载的模式。核心工具选用iText7的html2pdf组件,它能够直接将HTML/CSS转换为PDF,极大降低了排版难度。

在Maven项目的pom.xml中引入必要的依赖包。请确保版本号一致,避免因版本不兼容导致的类找不到异常。




com.itextpdf
html2pdf
4.0.5



com.itextpdf
font-asian
7.2.5


解决中文字体显示异常问题

在档案打印中,最常见的问题是生成的PDF中文显示为方块或乱码。这是因为HTML转PDF时默认没有加载中文字体。我们需要创建一个字体提供程序,指定系统字体或项目资源目录下的字体文件。

请在项目的src/main/resources/fonts目录下放入一个中文字体文件,例如SimSun.ttf(宋体)。以下是字体工具类的完整代码:

综合档案管理系统打印管理全流程实操指南

package com.archives.print.util;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.layout.font.FontProvider;
import java.io.IOException;
public class CustomFontProvider extends FontProvider {
public CustomFontProvider() {
try {
// 1. 注册宋体,用于正文
FontProgram fontProgram = FontProgramFactory.createFont("src/main/resources/fonts/SimSun.ttf");
this.addFont(fontProgram);
// 2. 注册黑体,用于标题(可选)
// FontProgram simHei = FontProgramFactory.createFont("src/main/resources/fonts/SimHei.ttf");
// this.addFont(simHei);
System.out.println("字体加载成功");
} catch (IOException e) {
System.err.println("字体加载失败,请检查文件路径:" + e.getMessage());
throw new RuntimeException("字体初始化异常", e);
}
}
}

设计档案打印HTML模板

为了方便维护,我们将打印模板设计为HTML字符串。在实际开发中,建议使用FreeMarker或Thymeleaf模板引擎,这里为了演示零门槛操作,直接使用Java字符串拼接。模板中使用了内联CSS来控制A4纸的边距和打印样式。

public class PrintTemplate {
public static String getArchiveCover(String archiveId, String title, String date, String secret) {
return "" +
"" +
"" +
"" +
"" +
"" +
"" +
"  
档案案卷封面
" + "
" + " 案卷题名:" + " " + title + "" + "
" + "
" + " 档号:" + " " + archiveId + "" + "
" + "
" + " 归档日期:" + " " + date + "" + "
" + "
" + " 密级:" + " " + secret + "" + "
" + "" + ""; } }

核心打印服务实现

这是打印管理的核心部分。我们需要编写一个服务类,将HTML源码转换为PDF字节数组。该类会调用前面配置的CustomFontProvider来解决中文乱码问题。

package com.archives.print.service;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.archives.print.util.CustomFontProvider;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Service
public class ArchivePrintService {
/
将HTML内容转换为PDF字节数组
@param htmlContent HTML源码
@return PDF文件的二进制数据
/
public byte[] generatePdfBytes(String htmlContent) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
// 1. 创建转换属性配置
ConverterProperties properties = new ConverterProperties();
// 2. 设置自定义字体提供器(关键步骤)
properties.setFontProvider(new CustomFontProvider());
// 3. 设置BaseUri,用于解析HTML中的相对路径资源(如图片),这里设为空
properties.setBaseUri("");
// 4. 执行转换
HtmlConverter.convertToPdf(htmlContent, outputStream, properties);
return outputStream.toByteArray();
} catch (IOException e) {
System.err.println("PDF生成失败:" + e.getMessage());
throw new RuntimeException("打印服务异常", e);
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

打印日志管理(业务层)

既然是“打印管理”,除了生成文件,还需要记录谁在什么时候打印了什么档案。这里展示一个简单的业务逻辑,包含模拟的数据库保存操作。

package com.archives.print.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class PrintLogService {
@Autowired
private ArchivePrintService archivePrintService;
// 模拟的日志记录方法
private void saveLog(String userId, String archiveId) {
// TODO: 将以下信息插入到数据库表 t_print_log
System.out.println("记录打印日志 -> 用户: " + userId + ", 档案ID: " + archiveId + ", 时间: " + new Date());
}
/
综合处理打印业务:生成PDF并记录日志
/
public byte[] handleArchivePrint(String userId, String archiveId, String title, String date, String secret) {
// 1. 生成HTML
String html = PrintTemplate.getArchiveCover(archiveId, title, date, secret);
// 2. 转换PDF
byte[] pdfBytes = archive.generatePdfBytes(html);
// 3. 记录日志
saveLog(userId, archiveId);
return pdfBytes;
}
}

控制层接口开发

我们需要暴露一个HTTP接口供前端调用。该接口接收档案参数,返回PDF文件流,浏览器会自动触发下载或预览。

package com.archives.print.controller;
import com.archives.print.service.PrintLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.;
@RestController
@RequestMapping("/api/print")
public class PrintController {
@Autowired
private PrintLogService printLogService;
@PostMapping("/archive")
public ResponseEntity printArchiveCover(
@RequestParam String userId,
@RequestParam String archiveId,
@RequestParam String title,
@RequestParam String date,
@RequestParam String secret) {
try {
// 调用业务层获取PDF数据
byte[] pdfContents = printLogService.handleArchivePrint(userId, archiveId, title, date, secret);
// 设置响应头,指定文件名和内容类型
String filename = "Archive_" + archiveId + ".pdf";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDispositionFormData("attachment", filename); // attachment表示下载,inline表示预览
headers.setContentLength(pdfContents.length);
return new ResponseEntity<>(pdfContents, headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

前端调用与测试

后端完成后,前端无需安装任何插件,直接通过请求接口即可实现打印。以下是一个纯HTML/JS的测试页面代码:





档案打印测试</title&rt;
</head>
<body>
<h2>档案打印管理测试</h2>
<button onclick="printArchive()">打印档案封面</button>
<script>
function printArchive() {
// 构造请求参数
const params = new URLSearchParams();
params.append('userId', 'user001');
params.append('archiveId', 'ARC-2023-001');
params.append('title', '2023年度财务审计报告');
params.append('date', '2023-12-31');
params.append('secret', '内部');
// 发送POST请求
fetch('/api/print/archive', {
method: 'POST',
body: params
})
.then(response => {
if (response.ok) {
return response.blob();
}
throw new Error('网络响应异常');
})
.then(blob => {
// 创建下载链接并触发点击
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = '档案封面.pdf';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
alert('打印请求已发送,文件正在下载');
})
.catch(error => {
console.error('打印失败:', error);
alert('打印失败,请检查后台服务');
});
}
</script>
</body>
</html>
</code></pre>
<h3>实施注意事项</h3>
<ul>
<li><strong>字体路径校验:</strong>请务必确认<code>CustomFontProvider</strong>中的字体路径在服务器上是绝对存在的,否则会抛出异常。</li>
<li><strong>CSS兼容性:</strong>iText对CSS的支持是子集,复杂的CSS3特性(如Flexbox、Grid)可能无法完美渲染,建议使用基础的Table布局或简单的Block布局。</li>
<li><strong>图片资源:</strong>如果HTML中包含图片,<code>ConverterProperties.setBaseUri</strong>必须设置为图片可访问的HTTP地址或本地文件路径前缀。</li>
<li><strong>性能优化:</strong>对于高并发打印场景,建议将<code>FontProvider</strong>配置为单例Bean,避免每次请求都重新读取字体文件。</li>
</ul>        </div>

        <!-- 上下篇 -->
        <div class="news-nav yfda-cnd3go-umoa-0vw5">
            <div class="news-nav-item yfda-cnhlcb-jjfq-eybe">
                    上一篇:<a href="https://www.yfda.cn/hyxx/39670.html">钢铁行业技术档案管理软件选品+落地全攻略</a>            </div>
            <div class="news-nav-item yfda-cnhlcb-jjfq-eybe">
                    下一篇: <a href="https://www.yfda.cn/hyxx/39678.html">2024主流电子档案系统排行榜及零基础本地部署实操全指南</a>            </div>
        </div>
        <div class="page-header yfda-cngd0u-lteg-m852">
            <div class="page-subtitle yfda-cnym1c-tntd-i8p0">NEWS</div>
            <div class="page-title yfda-cnzpra-jjkk-swym">相关信息</div>
            <div class="page-divider yfda-cnf7bx-okgy-08ks"></div>
        </div>
        <div class="news-li yfda-cnaowo-qyav-dwme">
            <article class="news-it yfda-cn5jjz-qfpm-r1ty">
                <div class="news-pi yfda-cnkfmo-dtsl-d4er">
                    <img src="https://www.yfda.cn/uploadfile/allimg/202606/2676d1bce4ee.jpeg" alt="综合档案管理系统,别再让资料乱成一锅粥了">
                </div>
                <div class="news-co yfda-cna7wb-odjg-lx44">
                    <a href="https://www.yfda.cn/hyxx/39688.html" class="news-ti yfda-cn0xx9-vmjn-ergv">综合档案管理系统,别再让资料乱成一锅粥了</a>
                    <div class="news-de yfda-cnrj52-jfzm-k23i">你有没有发现,很多单位,甭管大小,一提档案管理就头疼。纸质文件堆成山,找个去年的合同得翻半天;电子文件倒是方便,可U盘、电脑、网盘到处存,版本对不上是常事,一不小心还误删了。这事儿吧,说白了就是缺一套...</div>
                    <div class="news-me yfda-cntabv-todi-sn6x">2026年06月17日 03:05:20</div>
                </div>
            </article>
                        <article class="news-it yfda-cnqze4-itxg-u87s">
                <div class="news-pi yfda-cn09fz-jivb-3rkz">
                    <img src="https://www.yfda.cn/uploadfile/allimg/202606/6da2b3d060f44.jpeg" alt="档案软件单机版虚拟库房怎么选?手把手教你搭建不用联网的数字仓库">
                </div>
                <div class="news-co yfda-cn0fo0-babh-gn5i">
                    <a href="https://www.yfda.cn/hyxx/39686.html" class="news-ti yfda-cnfz43-xrta-od51">档案软件单机版虚拟库房怎么选?手把手教你搭建不用联网的数字仓库</a>
                    <div class="news-de yfda-cnwxyp-dnlp-fwoe">开头场景:老板突然要找三年前的合同,你慌不慌?</div>
                    <div class="news-me yfda-cndm16-ofwy-zilm">2026年06月17日 03:05:20</div>
                </div>
            </article>
                        <article class="news-it yfda-cn2me2-lqsa-vwrr">
                <div class="news-pi yfda-cn96ml-zzwz-km25">
                    <img src="https://www.yfda.cn/uploadfile/allimg/202606/634d88ee7a8d854.jpeg" alt="档案系统对比:2026年企业与个人场景下的选型指南">
                </div>
                <div class="news-co yfda-cnut19-pxbc-9kdf">
                    <a href="https://www.yfda.cn/hyxx/39684.html" class="news-ti yfda-cn5tqj-gfln-6zxu">档案系统对比:2026年企业与个人场景下的选型指南</a>
                    <div class="news-de yfda-cniicp-inix-a0ka">开篇直答</div>
                    <div class="news-me yfda-cn170g-uvza-sb21">2026年06月17日 03:05:20</div>
                </div>
            </article>
                        <article class="news-it yfda-cnl2wp-rvqq-eta8">
                <div class="news-pi yfda-cnpdja-yqxa-x07d">
                    <img src="https://www.yfda.cn/uploadfile/allimg/202606/137c1ee3ac58b8f.jpeg" alt="数字档案系统整理全流程指南 帮你彻底摆脱低效档案管理噩梦">
                </div>
                <div class="news-co yfda-cnpj5o-hgka-8tqv">
                    <a href="https://www.yfda.cn/hyxx/39682.html" class="news-ti yfda-cnbgx6-sjop-t68c">数字档案系统整理全流程指南 帮你彻底摆脱低效档案管理噩梦</a>
                    <div class="news-de yfda-cny3vz-khpj-6fcy">咱作为干了5年国企行政档案岗的老油条,最近被好几个同行追着问数字档案系统整理的方法,说之前要么搞成半拉子工程,要么做完还是不好用,问我有没有啥踩过坑的经验分享。那必须有啊,我当年为了搞数字档案系统整理...</div>
                    <div class="news-me yfda-cnl6ap-gqtp-eoii">2026年06月17日 03:05:20</div>
                </div>
            </article>
                        <article class="news-it yfda-cn4xf5-mxpg-s8iq">
                <div class="news-pi yfda-cn3rav-nbhv-7goj">
                    <img src="https://www.yfda.cn/uploadfile/allimg/202606/8440ad67265b7a1.jpeg" alt="2024主流电子档案系统排行榜及零基础本地部署实操全指南">
                </div>
                <div class="news-co yfda-cno9i2-wuts-kyu0">
                    <a href="https://www.yfda.cn/hyxx/39678.html" class="news-ti yfda-cnea9a-mgau-7opq">2024主流电子档案系统排行榜及零基础本地部署实操全指南</a>
                    <div class="news-de yfda-cnn5js-loys-pb4r">一、2024电子档案系统官方排行榜(分开源/商用)</div>
                    <div class="news-me yfda-cnxh7v-migg-cmgt">2026年06月17日 03:05:20</div>
                </div>
            </article>
                        <article class="news-it yfda-cnqypc-nccj-lott">
                <div class="news-pi yfda-cn3qqs-bfbp-rsrr">
                    <img src="https://www.yfda.cn/uploadfile/allimg/202606/161c265ef3d62f7.jpeg" alt="综合档案管理系统打印管理全流程实操指南">
                </div>
                <div class="news-co yfda-cngqzd-lfmk-2vcp">
                    <a href="https://www.yfda.cn/hyxx/39675.html" class="news-ti yfda-cnn2lc-rmvq-tp30">综合档案管理系统打印管理全流程实操指南</a>
                    <div class="news-de yfda-cnncny-bavm-e86x">技术选型与核心依赖配置</div>
                    <div class="news-me yfda-cn8txo-mgib-bpz2">2026年06月17日 03:05:20</div>
                </div>
            </article>
                    </div>
    </div>
    <!-- 底部 -->
    <footer>
        <div class="footer-container yfda-cng4al-liff-56fm">

            <div class="footer-col yfda-cnhqzw-vadc-admj">
                <h4>业务中心</h4>
                <ul>
                    <li><a href="/datxjs/">档案体系建设</a></li>
                    <li><a href="/dapx/">档案培训</a></li>
                    <li><a href="/daszh/">档案数字化</a></li>
                    <li><a href="/dazl/">档案整理</a></li>
                    <li><a href="/darj/">档案软件单机版</a></li>
                    <li><a href="/darjwlb/">数字档案馆系统</a></li>
                </ul>
            </div>
            <div class="footer-col yfda-cnhqzw-vadc-admj">
                <h4>关于云丰</h4>
                <ul>
                    <li><a href="/qyjs/">企业介绍</a></li>
                    <li><a href="/qywh/">企业文化</a></li>
                    <li><a href="/zpgw/">加入云丰</a></li>
                </ul>
            </div>
            <div class="footer-col yfda-cng4ob-niml-pgz5">
                <h4>联系我们</h4>
                <div class="contact-item yfda-cnmumn-msew-nc0o">
                                <img src="/style/images/dhb.svg" alt="ico图标" class="dbico1 yfda-cns8ad-gccw-np87">
                    <span>028-8744 4417</span>
                </div>
                <div class="contact-item yfda-cnmumn-msew-nc0o">
                                <img src="/style/images/sj.svg" alt="ico图标" class="dbico2 yfda-cnccg3-djrq-3fz4">
                    <span>1822 405 1822</span>
                </div>
                <div class="contact-item yfda-cnxngp-hjgg-gajv">
                                <img src="/style/images/qq1.svg" alt="ico图标" class="dbico3 yfda-cn38ko-rxjq-gcsy">
                    <span>2305721818</span>
                </div>
                <div class="contact-item yfda-cnlf2p-rwci-jq38">
                                <img src="/style/images/dz.svg" alt="ico图标" class="dbico4 yfda-cnase4-nrjc-mizp">
                    <span>四川省成都市高新区天府三街新希望国际B座</span>
                </div>
            </div>
            <div class="footer-col yfda-cnbdwt-dbng-6wjy">
                <div class="qrcode yfda-cn063h-dzkb-mcj6">
                    <img src="/style/images/ewm.jpg" alt="二维码">
                    <p>扫一扫上面的二维码图案,加我为朋友。</p>
                </div>
            </div>
        </div>
        <div class="copyright yfda-cnxh40-afhd-7140">
            版权所有 © 成都云丰档案管理咨询有限公司 | 蜀ICP备14004237号-2
        </div>
    </footer>
<div class="com-right-slide yfda-cnq3mh-dbng-bhub">
        <!-- 在线咨询 -->
        <div class="right-item right-item-container yfda-cnb8bl-qyid-zoan">
            <div class="items right-item-2 flex-col yfda-cn9bf6-qbyg-ipod">
                <div class="flex-col item-2 item-wxzx yfda-cn1p2x-kxbl-rrgi" style="border-bottom: 1px solid #eee;">
                    <img src="/style/images/icon_wx.svg" alt="" class="n-hover yfda-cna9if-wypt-9ns9">
                    <img src="/style/images/icon_wx-b.svg" alt="" class="hover yfda-cnj6ny-uhca-6qpl">
                    <span class="txt yfda-cn7mlg-slfb-hstp">微信咨询</span>
                </div>
                <div class="flex-col item-2 item-shfw yfda-cn8d0i-sial-2hy7">
                    <img src="/style/images/icon_shfw.svg" alt="" class="n-hover yfda-cna9if-wypt-9ns9">
                    <img src="/style/images/icon_shfw-b.svg" alt="" class="hover yfda-cnj6ny-uhca-6qpl">
                    <span class="txt yfda-cn7mlg-slfb-hstp">电话联系</span>
                </div>
                <div class="flex-col item-2 item-grh yfda-cnrbhj-hwpo-8vbu">
                    <img src="/style/images/icon_phone.svg" alt="" class="n-hover yfda-cn47vd-ugwf-igyk">
                    <img src="/style/images/icon_phone_b.svg" alt="" class="hover yfda-cnuvm8-uliq-lr8n">
                    <span class="txt yfda-cn5gat-zzwo-c9jr">QQ客服</span>

                </div>
            </div>
            <div class="slide-box sqzx-box flex-col yfda-cna60s-sglq-czqr">
                <img src="/style/images/ewm.jpg" alt="" class="qr-img yfda-cn6fus-yeeq-q1rs">
                <span class="dis day yfda-cn1hoc-pkxl-0r4f">微信咨询一对一服务</span>
            </div>
            <div class="slide-box shfw-box flex-col yfda-cnxgeu-mzmu-6m2t">
                <div class="flex yfda-cn537q-ovtl-dmkl">
                    <div class="flex-col content yfda-cn9gnk-vbds-tk58">
                        <span>服务热线:</span>
                        <span class="color-b yfda-cnywza-ddaw-tq1s">028-8744 4417</span>
                    </div>
                </div>
                </div>
            <div class="slide-box grh-box flex-col yfda-cn9nj0-klzi-fmp9">
                <div class="flex yfda-cnl59w-vtyo-9iq7">
                    <div class="flex-col content yfda-cnr2wa-pheq-r0uf">
                        <span>QQ客服:</span>
                        <span class="color-b yfda-cnpcjg-xtgj-2ahz">2305721818</span>
                    </div>
            </div>
        </div>
        </div>
        </div><script src="/style/global.js" type="text/javascript"></script>
    </body></html>