目中要求在移动端实现在线预览pdf文件,通过一番折腾,最后选择用pdf.js实现。
1、下载pdf.js
官网地址:https://mozilla.github.io/pdf.js/
2、各种配置
下载下来的文件包,就是一个demo,我们仿照这个demo做就可以啦
(1)页面元素如下:
<button class="product-term to-clause" id="noteDetail">《投保须知》</button> <button class="to-clause" id="clauseDetail">《保险条款》</button>
(2)js代码如下:
$('#clauseDetail').click(function () {
window.open('../viewer.html?file=xxx-clause.pdf');
});
注意:viewer.html就是下载下来文件包中的那个viewer.html,在此html中需要引入viewer.css、
locale.properties、pdf.js和viewer.js。修改viewer.js中的以下代码:
var DEFAULT_URL = 'compressed.tracemonkey-pldi-09.pdf';修改为 var DEFAULT_URL = '';
需要预览的pdf文件,就是window.open('../viewer.html?file=xxx-clause.pdf');中的xxx-clause.pdf文件。注意:pdf文件需要和
viewer.html放在同一个目录下,如果不在同一个目录下,需修改路径。
通过以上的配置,就实现了在线预览pdf文件。
数字化时代,HTML网页文件已成为我们获取和分享信息的主要方式。但有时,为了保留网页的格式和内容,我们可能需要将其转换为PDF文档。此时,首助编辑高手软件将是您的得力助手。下面,就让我们一起了解如何使用首助编辑高手将HTML网页文件转为PDF文档。
第一步,打开软件,在众多功能板块中,选择“PDF编辑工具”中的“批量转换格式”功能
第二步,在转换模式对应的下拉列表中,将选项切换为“HTML转PDF”
第三步,通过“添加文件”或者“添加文件夹中的文件”将HTML网页文件导入,支持导入多个
第四步,设置新文件保存位置,可以将新文件保存在原文件相同位置,也可以指定位置
第五步,确定选择好一系列相关的选项之后,单击右下角的“开始转换”按钮
第六步,提示转换成功后,打开文件夹查看,所有HTNL网页文件都被转为PDF文档,最后也可以打开原文件进行对比,原文件均为HTML,说明转换成功
总之,软件以其简洁的操作界面、强大的功能和高效的转换速度,成为了将HTML网页文件转为PDF文档的理想选择。无论您是学生、上班族还是专业人士,都能满足您的需求,让您的文档处理变得更加轻松和高效。赶快下载并体验首助编辑高手吧!
多编程技术文章,请查阅IOKKS - 专业编程技术分享平台
在本文中,我们将确保读取我们放在指定文件夹中的HTML文件,并解析其中的变量并替换为它们的实际值。然后,我使用了"openhtmltopdf-pdfbox"库修改了HTML文件。我们将介绍如何将其转换为PDF文件。
首先,我们将读取我们确定的文件夹下的HTML文件,解析它,并将我们自己的动态值传递给HTML中相关的变量。我们将使用"openhtmltopdf-pdfbox"库中最新更新的形式将HTML文件转换为PDF文件。
我希望这将成为需要这方面帮助的人的参考。您可以在您的Java项目中轻松进行转换。您可以在下面看到一个示例项目。
首先,我们将创建一个新的输入文件夹,我们将在其中读取我们的输入HTML文件,并创建一个输出文件夹,我们将在其中写入PDF文件。
我们可以将HTML文件放在输入文件夹下。我们定义一个要在HTML文件中替换的键值。这个键值被给定为#NAME#作为示例。在Java中,您可以选择用外部发送的值替换您想要的键值。
input folder : \ConvertHtmlToPDF\input
output folder: \ConvertHtmlToPDF\output
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta data-fr-http-equiv="Content-Type" content="text/html; charset=UTF-8">
</meta>
<title>Convert to Html to Pdf</title>
<style type="text/css">
body {
font-family: "Times New Roman", Times, serif;
font-size: 40px;
}
</style>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<table width="700" border="0" cellspacing="0" cellpadding="0" style="background-color: #1859AB;
color: white;
font-size: 14px;
border-radius: 1px;
line-height: 1em; height: 30px;">
<tbody>
<tr>
<td>
<strong style="color:#F8CD00;"> Hello </strong>#NAME#
</td>
</tr>
</tbody>
</table>
</body>
</html>
我们正在创建一个新的Spring项目。我正在使用Intellj Idea。
为了在HTML中用值替换键,我们将从外部发送值。我们为此编写一个rest服务。
我们在Controller文件夹下创建"ConvertHtmlToPdfController.java"类。我们在Controller类中创建一个名为"convertHtmlToPdf"的get方法。我们可以动态地将值传递给这个方法,如下所示。
package com.works.controller;
import com.works.service.ConvertHtmlToPdfService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("")
public class ConvertHtmlToPdfController {
private final ConvertHtmlToPdfService convertHtmlToPdfService;
public ConvertHtmlToPdfController(ConvertHtmlToPdfService convertHtmlToPdfService) {
this.convertHtmlToPdfService = convertHtmlToPdfService;
}
@GetMapping("/convertHtmlToPdf/{variableValue}")
public ResponseEntity<String> convertHtmlToPdf(@PathVariable @RequestBody String variableValue) {
try {
return ResponseEntity.ok(convertHtmlToPdfService.convertHtmlToPdf(variableValue));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
ConvertHtmlToPdfService.java服务包含一个名为convertHtmlToPdf的方法。convertHtmlToPdf方法接受字符串variableValue输入。
在convertHtmlToPdf服务方法中;
"inputFile"变量被定义为读取输入文件夹下的html文件。我们可以给这个变量赋予我们将要读取的输入html文件的URL。
"outputFile"变量被定义为将pdf文件分配给输出文件夹。我们可以将输出文件夹的URL赋给这个变量。
您还可以从外部读取字体文件。您可以从输入文件夹下获取这个文件。我们还可以将字体文件所在的URL分配给"fontFile"变量。
在上述代码行中,将包含输入的文件夹的URL传递给"ConvertHtmlToPdfUtil.readFileAsString"方法,以读取输入文件夹中的HTML文件。
String htmlContent = ConvertHtmlToPdfUtil.readFileAsString(inputFile);
package com.works.util;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class ConvertHtmlToPdfUtil {
public static void safeCloseBufferedReader(BufferedReader bufferedReader) throws Exception {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
throw new Exception("safeCloseBufferedReader - the method got an error. " + e.getMessage());
}
}
public static String readFileAsString(String filePath) throws Exception {
BufferedReader br = null;
String encoding = "UTF-8";
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding));
StringBuilder fileContentBuilder = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
if (fileContentBuilder.length() > 0) {
fileContentBuilder.append(System.getProperty("line.separator"));
}
fileContentBuilder.append(line);
}
return fileContentBuilder.toString();
} catch (Exception e) {
new Exception("readFileAsString - the method got an error." + e.getMessage(), e);
return null;
} finally {
safeCloseBufferedReader(br);
}
}
public static OutputStream htmlConvertToPdf(String html, String filePath, String fonts) throws Exception {
OutputStream os = null;
try {
os = new FileOutputStream(filePath);
final PdfRendererBuilder pdfBuilder = new PdfRendererBuilder();
pdfBuilder.useFastMode();
pdfBuilder.withHtmlContent(html, null);
String fontPath = fonts;
pdfBuilder.useFont(new File(concatPath(fontPath, "times.ttf")), "Times", null, null, false);
pdfBuilder.toStream(os);
pdfBuilder.run();
os.close();
} catch (Exception e) {
throw new Exception(e.getMessage(), e);
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
}
}
return os;
}
public static String concatPath(String path, String... subPathArr) {
for (String subPath : subPathArr) {
if (!path.endsWith(File.separator)) {
path += File.separator;
}
path += subPath;
}
return path;
}
}
以上是ConvertHtmlToPdfUtil.readFileAsString方法中的代码。它使用FileInputStream将HTML文件读取为字符集,并使用InputStreamReader将其转换为内部缓冲区,并使用BufferedReader将其放入内部缓冲区。
BufferedReader中的字符逐行读取,如下所示。所有HTML内容都被放入字符串变量中。使用safeCloseBufferedReader方法,当我们完成时,我们关闭缓冲区。
我们可以将我们的HTML内容发送到setVariableValue方法中,以便用我们从外部发送的值替换我们在服务中发送的值。我们在HTML中标记为#key#的键值将被值值替换。
private String setVariableValue(String htmlContent, String key, String value) {
if (StringUtils.isNotEmpty(value)) {
htmlContent = htmlContent.replaceAll("#"+key+"#", value);
}else {
htmlContent = htmlContent.replaceAll("#"+key+"#", "");
}
return htmlContent;
}
然后,在替换过程之后,我们可以调用ConvertHtmlToPdfUtil.htmlConvertToPdf方法,将HTML URL文件生成为PDF输出。ConvertHtmlToPdfUtil.htmlConvertToPdf方法可以接收HTML内容、输出和字体输入,如下所示。
我们可以将这些输入传递给该方法。
ConvertHtmlToPdfUtil.htmlConvertToPdf(htmlContent, outputFile, fontFile);
ConvertHtmlToPdfUtil.htmlConvertToPdf方法内容;
我们将创建一个新的FileOutputStream。这将确定我们指定的output.pdf文件的创建。
PdfRendererBuilder类位于com.openhtmltopdf.pdfboxout库中。因此,我们必须将此库添加到pom.xml文件中,如下所示。
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>1.0.10</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.works</groupId>
<artifactId>convertHtmlToPDF</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>convertHtmlToPDF</name>
<description>convertHtmlToPDF</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
实现PdfRendererBuilder对象后,我们可以将HTML参数设置为'withHtmlContent',将fontpath参数设置为'useFont'。我们可以使用toStream设置输出文件。最后,我们可以使用run方法运行它。
pdfBuilder.useFastMode();
pdfBuilder.withHtmlContent(html, null);
String fontPath = fonts;
pdfBuilder.useFont(new File(concatPath(fontPath, "times.ttf")), "Times", null, null, false);
pdfBuilder.toStream(os);
pdfBuilder.run();
pdfBuilder.run(); 在运行该方法后,我们应该看到output.pdf文件被创建在output文件夹下。
因此,我们可以看到使用openhtmltopdf-pdfbox库进行平滑的HTML到PDF转换过程。
*请认真填写需求信息,我们会在24小时内与您取得联系。