Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
目中要求在移動(dòng)端實(shí)現(xiàn)在線預(yù)覽pdf文件,通過一番折騰,最后選擇用pdf.js實(shí)現(xiàn)。
1、下載pdf.js
官網(wǎng)地址:https://mozilla.github.io/pdf.js/
2、各種配置
下載下來的文件包,就是一個(gè)demo,我們仿照這個(gè)demo做就可以啦
(1)頁(yè)面元素如下:
<button class="product-term to-clause" id="noteDetail">《投保須知》</button> <button class="to-clause" id="clauseDetail">《保險(xiǎn)條款》</button>
(2)js代碼如下:
$('#clauseDetail').click(function () {
window.open('../viewer.html?file=xxx-clause.pdf');
});
注意:viewer.html就是下載下來文件包中的那個(gè)viewer.html,在此html中需要引入viewer.css、
locale.properties、pdf.js和viewer.js。修改viewer.js中的以下代碼:
var DEFAULT_URL = 'compressed.tracemonkey-pldi-09.pdf';修改為 var DEFAULT_URL = '';
需要預(yù)覽的pdf文件,就是window.open('../viewer.html?file=xxx-clause.pdf');中的xxx-clause.pdf文件。注意:pdf文件需要和
viewer.html放在同一個(gè)目錄下,如果不在同一個(gè)目錄下,需修改路徑。
通過以上的配置,就實(shí)現(xiàn)了在線預(yù)覽pdf文件。
數(shù)字化時(shí)代,HTML網(wǎng)頁(yè)文件已成為我們獲取和分享信息的主要方式。但有時(shí),為了保留網(wǎng)頁(yè)的格式和內(nèi)容,我們可能需要將其轉(zhuǎn)換為PDF文檔。此時(shí),首助編輯高手軟件將是您的得力助手。下面,就讓我們一起了解如何使用首助編輯高手將HTML網(wǎng)頁(yè)文件轉(zhuǎn)為PDF文檔。
第一步,打開軟件,在眾多功能板塊中,選擇“PDF編輯工具”中的“批量轉(zhuǎn)換格式”功能
第二步,在轉(zhuǎn)換模式對(duì)應(yīng)的下拉列表中,將選項(xiàng)切換為“HTML轉(zhuǎn)PDF”
第三步,通過“添加文件”或者“添加文件夾中的文件”將HTML網(wǎng)頁(yè)文件導(dǎo)入,支持導(dǎo)入多個(gè)
第四步,設(shè)置新文件保存位置,可以將新文件保存在原文件相同位置,也可以指定位置
第五步,確定選擇好一系列相關(guān)的選項(xiàng)之后,單擊右下角的“開始轉(zhuǎn)換”按鈕
第六步,提示轉(zhuǎn)換成功后,打開文件夾查看,所有HTNL網(wǎng)頁(yè)文件都被轉(zhuǎn)為PDF文檔,最后也可以打開原文件進(jìn)行對(duì)比,原文件均為HTML,說明轉(zhuǎn)換成功
總之,軟件以其簡(jiǎn)潔的操作界面、強(qiáng)大的功能和高效的轉(zhuǎn)換速度,成為了將HTML網(wǎng)頁(yè)文件轉(zhuǎn)為PDF文檔的理想選擇。無論您是學(xué)生、上班族還是專業(yè)人士,都能滿足您的需求,讓您的文檔處理變得更加輕松和高效。趕快下載并體驗(yàn)首助編輯高手吧!
多編程技術(shù)文章,請(qǐng)查閱IOKKS - 專業(yè)編程技術(shù)分享平臺(tái)
在本文中,我們將確保讀取我們放在指定文件夾中的HTML文件,并解析其中的變量并替換為它們的實(shí)際值。然后,我使用了"openhtmltopdf-pdfbox"庫(kù)修改了HTML文件。我們將介紹如何將其轉(zhuǎn)換為PDF文件。
首先,我們將讀取我們確定的文件夾下的HTML文件,解析它,并將我們自己的動(dòng)態(tài)值傳遞給HTML中相關(guān)的變量。我們將使用"openhtmltopdf-pdfbox"庫(kù)中最新更新的形式將HTML文件轉(zhuǎn)換為PDF文件。
我希望這將成為需要這方面幫助的人的參考。您可以在您的Java項(xiàng)目中輕松進(jìn)行轉(zhuǎn)換。您可以在下面看到一個(gè)示例項(xiàng)目。
首先,我們將創(chuàng)建一個(gè)新的輸入文件夾,我們將在其中讀取我們的輸入HTML文件,并創(chuàng)建一個(gè)輸出文件夾,我們將在其中寫入PDF文件。
我們可以將HTML文件放在輸入文件夾下。我們定義一個(gè)要在HTML文件中替換的鍵值。這個(gè)鍵值被給定為#NAME#作為示例。在Java中,您可以選擇用外部發(fā)送的值替換您想要的鍵值。
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>
我們正在創(chuàng)建一個(gè)新的Spring項(xiàng)目。我正在使用Intellj Idea。
為了在HTML中用值替換鍵,我們將從外部發(fā)送值。我們?yōu)榇司帉懸粋€(gè)rest服務(wù)。
我們?cè)贑ontroller文件夾下創(chuàng)建"ConvertHtmlToPdfController.java"類。我們?cè)贑ontroller類中創(chuàng)建一個(gè)名為"convertHtmlToPdf"的get方法。我們可以動(dòng)態(tài)地將值傳遞給這個(gè)方法,如下所示。
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服務(wù)包含一個(gè)名為convertHtmlToPdf的方法。convertHtmlToPdf方法接受字符串variableValue輸入。
在convertHtmlToPdf服務(wù)方法中;
"inputFile"變量被定義為讀取輸入文件夾下的html文件。我們可以給這個(gè)變量賦予我們將要讀取的輸入html文件的URL。
"outputFile"變量被定義為將pdf文件分配給輸出文件夾。我們可以將輸出文件夾的URL賦給這個(gè)變量。
您還可以從外部讀取字體文件。您可以從輸入文件夾下獲取這個(gè)文件。我們還可以將字體文件所在的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將其轉(zhuǎn)換為內(nèi)部緩沖區(qū),并使用BufferedReader將其放入內(nèi)部緩沖區(qū)。
BufferedReader中的字符逐行讀取,如下所示。所有HTML內(nèi)容都被放入字符串變量中。使用safeCloseBufferedReader方法,當(dāng)我們完成時(shí),我們關(guān)閉緩沖區(qū)。
我們可以將我們的HTML內(nèi)容發(fā)送到setVariableValue方法中,以便用我們從外部發(fā)送的值替換我們?cè)诜?wù)中發(fā)送的值。我們?cè)贖TML中標(biāo)記為#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;
}
然后,在替換過程之后,我們可以調(diào)用ConvertHtmlToPdfUtil.htmlConvertToPdf方法,將HTML URL文件生成為PDF輸出。ConvertHtmlToPdfUtil.htmlConvertToPdf方法可以接收HTML內(nèi)容、輸出和字體輸入,如下所示。
我們可以將這些輸入傳遞給該方法。
ConvertHtmlToPdfUtil.htmlConvertToPdf(htmlContent, outputFile, fontFile);
ConvertHtmlToPdfUtil.htmlConvertToPdf方法內(nèi)容;
我們將創(chuàng)建一個(gè)新的FileOutputStream。這將確定我們指定的output.pdf文件的創(chuàng)建。
PdfRendererBuilder類位于com.openhtmltopdf.pdfboxout庫(kù)中。因此,我們必須將此庫(kù)添加到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>
實(shí)現(xiàn)PdfRendererBuilder對(duì)象后,我們可以將HTML參數(shù)設(shè)置為'withHtmlContent',將fontpath參數(shù)設(shè)置為'useFont'。我們可以使用toStream設(shè)置輸出文件。最后,我們可以使用run方法運(yùn)行它。
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(); 在運(yùn)行該方法后,我們應(yīng)該看到output.pdf文件被創(chuàng)建在output文件夾下。
因此,我們可以看到使用openhtmltopdf-pdfbox庫(kù)進(jìn)行平滑的HTML到PDF轉(zhuǎn)換過程。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。