springboot使用itext生成pdf并保存到本地

使用html渲染页面,将页面转换为pdf文件,设置pdf页眉,页码,水印,目录,二维码

所需jar包

	<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
    <!--模板引擎相关依赖-->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.3.RELEASE</version>
        <scope>compile</scope>
    </dependency>

    <!--二维码 -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.3.3</version>
    </dependency>

    <!--pdf -->
    <dependency>
        <groupId>org.xhtmlrenderer</groupId>
        <artifactId>flying-saucer-pdf-itext5</artifactId>
        <version>9.1.18</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.7</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.7</version>
    </dependency>

代码片段

/**
* 渲染html文件
* 将html转换为pdf,并设置目录,添加页眉,水印,背景
* @param response
* @param request
*/
public void generatePdf(HttpServletResponse response, HttpServletRequest request){
Map<String, Object> param = htmlParam();
log.info(“获取到模板内容,开始渲染html模板”);
WebContext ctx = new WebContext(request, response, request.getServletContext(), request.getLocale());
ctx.setVariables(param);
String htmlContent = templateEngine.process(HtmlTemplateEnum.REPORT_CREDIT_BASE.getLocalUrl(), ctx);
log.info(“模板内容渲染完成,开始转换为临时pdf文件”);
String basepath = this.getClass().getClassLoader().getResource("./").getPath();
String tempPath = basepath + “temp”;
File filedir = new File(tempPath);
if (!filedir.exists()) {// 文件夹不存在则创建
filedir.mkdirs();
}
String sourceFile = PdfUtil.createPDF(basepath, tempPath, htmlContent);
log.info(“临时pdf文件文件生成成功”);
// 新的pdf地址
String pdfNumber = “PDF” + System.currentTimeMillis();
// 二维码图片地址
String orImgPath = tempPath + File.separator + pdfNumber + “.png”;
// 新pdf地址
String targetFile = tempPath + File.separator + pdfNumber + “.pdf”;
String fontPath = basepath + “static” + File.separator + “simsun.ttc”;
// 二维码图片内容
String content = “www.baidu.com”;
try {
QRCodeUtils.encodeToPath(content, 100, 100, “png”, new File(orImgPath).getPath());
// 生成新的pdf
PdfUtil.createPdfWithCatalog(sourceFile, targetFile, fontPath, pdfNumber, this.catalogs(), orImgPath, basepath, “pdf生成工具”);
log.info(“pdf文件生成成功,文件地址为:”+targetFile);
}catch (Exception e){
log.info(“pdf生成出错,错误信息:”+e.getMessage());
}finally {
// 删除创建的临时文件
File file = new File(sourceFile);
if (file.exists()) {
file.delete();
}
/File file1 = new File(targetFile);
if (file1.exists()) {
file1.delete();
}
/
File file2 = new File(orImgPath);
if (file2.exists()) {
file2.delete();
}
}

}

代码连接地址

链接: https://download.csdn.net/download/whyUnhappy/15513597.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用iTextPDF生成PDF文件时,可以通过以下几种方式来压缩PDF文件大小: 1. 压缩图片质量:可以使用iTextPDF提供的`Image`类中的`setCompressionLevel()`方法,将图片的压缩质量设置为0-9之间的整数,数字越大,压缩质量越低,文件大小也越小。 ```java Image image = Image.getInstance("image.jpg"); image.setCompressionLevel(9); ``` 2. 压缩字体:可以使用iTextPDF提供的`Font`类中的`setCompressionLevel()`方法,将字体的压缩质量设置为0-9之间的整数,数字越大,压缩质量越低,文件大小也越小。 ```java Font font = new Font(Font.HELVETICA, 12); font.setCompressionLevel(9); ``` 3. 压缩整个PDF文件:可以使用iTextPDF提供的`PdfWriter`类中的`setCompressionLevel()`方法,将PDF文件的压缩质量设置为0-9之间的整数,数字越大,压缩质量越低,文件大小也越小。 ```java PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); writer.setCompressionLevel(9); ``` 下面是一个完整的使用iTextPDF生成PDF文件并压缩大小的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; public class PdfGenerator { public static void main(String[] args) { Document document = new Document(); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); writer.setCompressionLevel(9); document.open(); Font font = new Font(Font.HELVETICA, 12); font.setCompressionLevel(9); Paragraph paragraph = new Paragraph("Hello, world!", font); document.add(paragraph); Image image = Image.getInstance("image.jpg"); image.setCompressionLevel(9); document.add(image); document.close(); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值