flying-saucer-pdf预览及下载

2024-05-06 23:08
文章标签 pdf 下载 预览 flying saucer

本文主要是介绍flying-saucer-pdf预览及下载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用这个库的初衷

找阿里编程规范的时候在github上搜索到的,觉得pdf像书籍一样的翻页比较自由,也是手痒吧。
启发开源项目github-pdf-demo

国人的EditMD比较不错我弄了一个MarkDown写作

页面是自己凑凑的看一下图:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

pdf模板页面

<!DOCTYPE html>
<html>
<head lang="en"><title>${title} - PDF</title><link href="http://localhost:8080/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/><link href="http://localhost:8080/css/pdfPage.css" rel="stylesheet" type="text/css"/><link href="http://localhost:8080/editormd/css/editormd.css" rel="stylesheet" type="text/css"/><style>@page {size: 210mm 297mm; /*设置纸张大小:A4(210mm 297mm)、A3(297mm 420mm) 横向则反过来*/margin: 0.25in;padding: 1em;@bottom-center {content: "Bamboo © 版权所有";font-family: SimSun;font-size: 12px;color: #000;};@top-center {content: element(header)};@bottom-right {content: "第" counter(page) "页  共 " counter(pages) "页";font-family: SimSun;font-size: 12px;color: #000;};}</style>
</head>
<body style="font-family: SimSun;">
<div class="container"><div class="projects-header page-header"><h2 id="pdftitle" class="text-center" style=" width: 70%;">${title!获取标题失败}</h2><p id="pdflen" class="text-center" style=" width: 70%;">字数:${len!'0'}</p></div><div class="row" style="white-space:normal;word-wrap:break-word;word-break:break-all;width: 70%;">${content!'获取内容失败'}</div></div>
</body>
</html>

github项目工具类

package com.bamboo.markdown.paper.common;import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.List;
import java.util.Map;/*** Created by admin on 2018/6/18.* ftl模板转pdf处理工具类*/
public class FtlTransPdfUtils {private static final Logger LOGGER = LoggerFactory.getLogger(FtlTransPdfUtils.class);/*** 按模板和参数生成html字符串,再转换为flying-saucer识别的Document** @param templateName freemarker模板名称* @param variables    freemarker模板参数* @return Document*/private static Document generateDoc(FreeMarkerConfigurer configurer, String templateName, Map<String, Object> variables) {Template tp;try {tp = configurer.getConfiguration().getTemplate(templateName);} catch (IOException e) {LOGGER.error(e.getMessage(), e);return null;}StringWriter stringWriter = new StringWriter();try (BufferedWriter writer = new BufferedWriter(stringWriter)) {try {tp.process(variables, writer);writer.flush();} catch (TemplateException e) {LOGGER.error("模板不存在或者路径错误", e);} catch (IOException e) {LOGGER.error("IO异常", e);}DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();return builder.parse(new ByteArrayInputStream(stringWriter.toString().getBytes()));} catch (Exception e) {LOGGER.error(e.getMessage(), e);return null;}}/*** 核心: 根据freemarker模板生成pdf文档** @param configurer   freemarker配置* @param templateName freemarker模板名称* @param out          输出流* @param listVars     freemarker模板参数* @throws Exception 模板无法找到、模板语法错误、IO异常*/private static void generateAll(FreeMarkerConfigurer configurer, String templateName, OutputStream out, List<Map<String, Object>> listVars) {if (CollectionUtils.isEmpty(listVars)) {LOGGER.warn("警告:freemarker模板参数为空!");return;}ITextRenderer renderer = new ITextRenderer();Document doc = generateDoc(configurer, templateName, listVars.get(0));renderer.setDocument(doc, null);//设置字符集(宋体),此处必须与模板中的<body style="font-family: SimSun">一致,区分大小写,不能写成汉字"宋体"ITextFontResolver fontResolver = renderer.getFontResolver();try {fontResolver.addFont("typeface/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);} catch (DocumentException e) {LOGGER.error("S1DocumentException" + e.getMessage());} catch (IOException e) {LOGGER.error("S2IOException" + e.getMessage());}//展现和输出pdfrenderer.layout();try {renderer.createPDF(out, false);} catch (DocumentException e) {LOGGER.error("S3DocumentException" + e.getMessage());}//根据参数集个数循环调用模板,追加到同一个pdf文档中//(注意:此处从1开始,因为第0是创建pdf,从1往后则向pdf中追加内容)for (int i = 1; i < listVars.size(); i++) {Document docAppend = generateDoc(configurer, templateName, listVars.get(i));renderer.setDocument(docAppend, null);renderer.layout();try {renderer.writeNextDocument(); //写下一个pdf页面} catch (DocumentException e) {LOGGER.error("S4DocumentException" + e.getMessage());}}renderer.finishPDF(); //完成pdf写入}/*** pdf下载** @param configurer   freemarker配置* @param templateName freemarker模板名称(带后缀.ftl)* @param listVars     模板参数集* @param response     HttpServletResponse* @param fileName     下载文件名称(带文件扩展名后缀)*/public static void download(FreeMarkerConfigurer configurer, String templateName, List<Map<String, Object>> listVars, HttpServletResponse response, String fileName) {// 设置编码、文件ContentType类型、文件头、下载文件名response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data");try {response.setHeader("Content-Disposition", "attachment;fileName=" +new String(fileName.getBytes("gb2312"), "ISO8859-1"));} catch (UnsupportedEncodingException e) {LOGGER.error("X1" + e.getMessage());}try (ServletOutputStream out = response.getOutputStream()) {generateAll(configurer, templateName, out, listVars);out.flush();} catch (Exception e) {LOGGER.error("X2" + e.getMessage());//X2java.io.IOException: 远程主机强迫关闭了一个现有的连接。}}/*** pdf预览** @param configurer   freemarker配置* @param templateName freemarker模板名称(带后缀.ftl)* @param listVars     模板参数集* @param response     HttpServletResponse*/public static void preview(FreeMarkerConfigurer configurer, String templateName, List<Map<String, Object>> listVars, HttpServletResponse response) {try (ServletOutputStream out = response.getOutputStream()) {generateAll(configurer, templateName, out, listVars);out.flush();} catch (Exception e) {LOGGER.error(e.getMessage(), e);}}
}

获取内容

package com.bamboo.markdown.paper.controller;import com.bamboo.markdown.paper.common.FtlTransPdfUtils;
import com.bamboo.markdown.paper.entity.vo.ArticleView;
import com.bamboo.markdown.paper.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.xml.sax.SAXException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** Created by admin on 2018/6/18.*/
@Controller
@RequestMapping(value = "/pdf")
public class PdfController {@Autowiredprivate FreeMarkerConfigurer configurer;@Autowiredprivate ArticleRepository articleRepository;/*** pdf预览** @param request  HttpServletRequest* @param response HttpServletResponse*/@RequestMapping(value = "/preview/{id}", method = RequestMethod.GET)public void preview(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer id) throws IOException, SAXException {ArticleView articleView = new ArticleView(articleRepository.findById(id));// 构造freemarker模板引擎参数,listVars.size()个数对应pdf页数List<Map<String, Object>> listVars = new ArrayList<>();Map<String, Object> variables = new HashMap<>();variables.put("title", articleView.getTitle());//补充修正String htmlContent = articleView.getHtmlMaterial().replace("<br>", "<br/>");//匹配img标签的正则表达式String regxpForImgTag = "<img\\s[^>]+>";Pattern pattern = Pattern.compile(regxpForImgTag);Matcher matcher = pattern.matcher(htmlContent);StringBuffer sbr = new StringBuffer();while (matcher.find()) {String temp = matcher.group();matcher.appendReplacement(sbr, temp.replace(">", " />"));}matcher.appendTail(sbr);String htmlMaterial = sbr.toString();String regxpForImgTag2 = "<pre><code\\s[^>]+>";Pattern pattern2 = Pattern.compile(regxpForImgTag2);Matcher matcher2 = pattern2.matcher(htmlMaterial);StringBuffer sbr2 = new StringBuffer();while (matcher2.find()) {String temp2 = matcher2.group();matcher2.appendReplacement(sbr2, temp2.replace("<pre><code class=\"lang-java\">"," <pre class=\"prettyprint linenums prettyprinted\" style=\"\"><code class=\"lang-java\">"));}matcher2.appendTail(sbr2);variables.put("content", sbr2.toString());variables.put("len", htmlMaterial.length());listVars.add(variables);//FtlTransPdfUtils.preview(configurer, "pdf/pdfPage.ftl", listVars, response);}/*** pdf下载** @param request  HttpServletRequest* @param response HttpServletResponse*/@RequestMapping(value = "/download/{id}", method = RequestMethod.GET)public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer id) {ArticleView articleView = new ArticleView(articleRepository.findById(id));List<Map<String, Object>> listVars = new ArrayList<>();Map<String, Object> variables = new HashMap<>();variables.put("title", articleView.getTitle());//补充修正String htmlContent = articleView.getHtmlMaterial().replace("<br>", "<br/>");//匹配img标签的正则表达式String regxpForImgTag = "<img\\s[^>]+>";Pattern pattern = Pattern.compile(regxpForImgTag);Matcher matcher = pattern.matcher(htmlContent);StringBuffer sbr = new StringBuffer();while (matcher.find()) {String temp = matcher.group();matcher.appendReplacement(sbr, temp.replace(">", " />"));}matcher.appendTail(sbr);String htmlMaterial = sbr.toString();String regxpForImgTag2 = "<pre><code\\s[^>]+>";Pattern pattern2 = Pattern.compile(regxpForImgTag2);Matcher matcher2 = pattern2.matcher(htmlMaterial);StringBuffer sbr2 = new StringBuffer();while (matcher2.find()) {String temp2 = matcher2.group();matcher2.appendReplacement(sbr2, temp2.replace("<pre><code class=\"lang-java\">"," <pre class=\"prettyprint linenums prettyprinted\" style=\"\"><code class=\"lang-java\">"));}matcher2.appendTail(sbr2);variables.put("content", sbr2.toString());variables.put("len", htmlMaterial.length());listVars.add(variables);FtlTransPdfUtils.download(configurer, "pdf/pdfPage.ftl", listVars, response, articleView.getTitle() + ".pdf");}
}

资源文件图,字体在github那个项目获得

这里写图片描述

这篇关于flying-saucer-pdf预览及下载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/965643

相关文章

SpringBoot返回文件让前端下载的几种方式

《SpringBoot返回文件让前端下载的几种方式》文章介绍了开发中文件下载的两种常见解决方案,并详细描述了通过后端进行下载的原理和步骤,包括一次性读取到内存和分块写入响应输出流两种方法,此外,还提供... 目录01 背景02 一次性读取到内存,通过响应输出流输出到前端02 将文件流通过循环写入到响应输出流

Python结合Free Spire.PDF for Python实现PDF页面旋转

《Python结合FreeSpire.PDFforPython实现PDF页面旋转》在日常办公或文档处理中,我们经常会遇到PDF页面方向错误的问题,本文将分享如何用Python结合FreeSpir... 目录基础实现:单页PDF精准旋转完整代码代码解析进阶操作:覆盖多场景旋转需求1. 旋转指定角度(90/27

使用C#实现将RTF转换为PDF

《使用C#实现将RTF转换为PDF》RTF(RichTextFormat)是一种通用的文档格式,允许用户在不同的文字处理软件中保存和交换格式化文本,下面我们就来看看如何使用C#实现将RTF转换为PDF... 目录Spire.Doc for .NET 简介安装 Spire.Doc代码示例处理异常总结RTF(R

SpringBoot集成iText快速生成PDF教程

《SpringBoot集成iText快速生成PDF教程》本文介绍了如何在SpringBoot项目中集成iText9.4.0生成PDF文档,包括新特性的介绍、环境准备、Service层实现、Contro... 目录SpringBoot集成iText 9.4.0生成PDF一、iText 9新特性与架构变革二、环

使用Python在PDF中绘制多种图形的操作示例

《使用Python在PDF中绘制多种图形的操作示例》在进行PDF自动化处理时,人们往往首先想到的是文本生成、图片嵌入或表格绘制等常规需求,然而在许多实际业务场景中,能够在PDF中灵活绘制图形同样至关重... 目录1. 环境准备2. 创建 PDF 文档与页面3. 在 PDF 中绘制不同类型的图形python

使用Python实现在PDF中添加、导入、复制、移动与删除页面

《使用Python实现在PDF中添加、导入、复制、移动与删除页面》在日常办公和自动化任务中,我们经常需要对PDF文件进行页面级的编辑,使用Python,你可以轻松实现这些操作,而无需依赖AdobeAc... 目录1. 向 PDF 添加空白页2. 从另一个 PDF 导入页面3. 删除 PDF 中的页面4. 在

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2

Python包管理工具uv下载python版本慢问题解决办法

《Python包管理工具uv下载python版本慢问题解决办法》uv是一个非常快的Python包和项目管理器,用Rust编写,使用热缓存安装Trio的依赖项的速度对比,:本文主要介绍Python包... 目录发现问题对于 MACOS / linux 用户 (zsh/bash):对于 Windows 用户:总

OFD格式文件及如何适应Python将PDF转换为OFD格式文件

《OFD格式文件及如何适应Python将PDF转换为OFD格式文件》OFD是中国自主研发的一种固定版式文档格式,主要用于电子公文、档案管理等领域,:本文主要介绍OFD格式文件及如何适应Python... 目录前言什么是OFD格式文档?使用python easyofd库将PDF转换为OFD第一步:安装 eas

基于Java实现PPT到PDF的高效转换详解

《基于Java实现PPT到PDF的高效转换详解》在日常开发中,经常会遇到将PPT文档批量或单文件转换为PDF的需求,本文将详细介绍其使用流程、核心代码与常见问题解决方案,希望对大家有所帮助... 目录一、环境配置Maven 配置Gradle 配置二、核心实现:3步完成PPT转PDF1. 单文件转换(基础版)