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
保存在線文章以便日后查閱,如何實(shí)現(xiàn)?想將網(wǎng)頁內(nèi)容轉(zhuǎn)為PDF保存,如何轉(zhuǎn)換?
今天,小福教大家如何將網(wǎng)頁生成PDF文件,跟著小福一起來看看吧!
首先,我們打開福昕高級(jí)PDF編輯器,選擇左上角“文件”>“創(chuàng)建”>“從網(wǎng)頁”,或點(diǎn)擊菜單欄“轉(zhuǎn)換”>“從網(wǎng)頁”;
在彈出的對(duì)話框中,粘貼轉(zhuǎn)換的網(wǎng)頁的URL鏈接,點(diǎn)擊“創(chuàng)建”,福昕高級(jí)PDF編輯器將開始加載網(wǎng)頁內(nèi)容;
加載完畢后,網(wǎng)頁就被轉(zhuǎn)為PDF啦!此時(shí),我們就可以對(duì)該頁面進(jìn)行進(jìn)一步的編輯和調(diào)整,如文字提取、編輯、添加注釋、劃重點(diǎn)等。完成編輯后,點(diǎn)擊保存即可。
此外,在轉(zhuǎn)換時(shí),我們可以點(diǎn)擊“設(shè)置”來選擇更多轉(zhuǎn)換選項(xiàng):
在彈出的“網(wǎng)頁轉(zhuǎn)換設(shè)置”對(duì)話框中,有“常規(guī)”和“頁面布局”選項(xiàng)卡。常規(guī)選項(xiàng)卡可以設(shè)置網(wǎng)頁的轉(zhuǎn)換等級(jí)和轉(zhuǎn)換內(nèi)容,頁面布局則可設(shè)置生成的PDF的頁面尺寸、頁邊距和方向,以及多媒體類型及縮放比例。
這里給大家說說常用的一些設(shè)置:
在“常規(guī)”選項(xiàng)卡中:
1、轉(zhuǎn)換等級(jí):可以指定網(wǎng)站中要轉(zhuǎn)換的網(wǎng)頁等級(jí),或勾選“轉(zhuǎn)換整個(gè)站點(diǎn)”。
轉(zhuǎn)換同一路徑的網(wǎng)頁:轉(zhuǎn)換隸屬于您輸入的網(wǎng)頁地址的所有網(wǎng)頁;轉(zhuǎn)換同一服務(wù)器上的網(wǎng)頁:轉(zhuǎn)換儲(chǔ)存在同一服務(wù)器上的所有網(wǎng)頁。
2、PDF設(shè)置:設(shè)置加載整個(gè)頁面的超時(shí)時(shí)間。超過此設(shè)置時(shí)間,加載進(jìn)程將自動(dòng)中止,且僅已加載內(nèi)容會(huì)被轉(zhuǎn)換成PDF。默認(rèn)值為120秒。
3、其他設(shè)置:
以上就是本期所有內(nèi)容,我們下期再見啦!
多編程技術(shù)文章,請(qǐng)查閱IOKKS - 專業(yè)編程技術(shù)分享平臺(tái)
在本文中,我們將確保讀取我們放在指定文件夾中的HTML文件,并解析其中的變量并替換為它們的實(shí)際值。然后,我使用了"openhtmltopdf-pdfbox"庫修改了HTML文件。我們將介紹如何將其轉(zhuǎn)換為PDF文件。
首先,我們將讀取我們確定的文件夾下的HTML文件,解析它,并將我們自己的動(dòng)態(tài)值傳遞給HTML中相關(guān)的變量。我們將使用"openhtmltopdf-pdfbox"庫中最新更新的形式將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庫中。因此,我們必須將此庫添加到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庫進(jìn)行平滑的HTML到PDF轉(zhuǎn)換過程。
github: https://github.com/rtfpessoa/diff2html
項(xiàng)目地址:https://diff2html.xyz/
在軟件開發(fā)和版本控制過程中,diff 命令是不可或缺的,它用于顯示文件之間的差異。然而,直接在命令行中查看 diff 的輸出可能不夠直觀,特別是對(duì)于大型文件或復(fù)雜的代碼更改。為了更友好地展示這些差異,diff2html 應(yīng)運(yùn)而生,它將 diff 的輸出轉(zhuǎn)換成易于閱讀的 HTML 格式,使得代碼審查、版本對(duì)比等任務(wù)變得更加高效。
diff2html 是一個(gè)輕量級(jí)的 JavaScript 庫,它可以將 diff(通常是 Git 或其他版本控制系統(tǒng)生成的補(bǔ)丁文件)轉(zhuǎn)換成漂亮的 HTML 格式。這個(gè)庫不僅支持基本的文本差異顯示,還提供了多種樣式和配置選項(xiàng),以滿足不同場(chǎng)景下的需求。
diff2html 可以通過多種方式安裝和使用,包括但不限于 npm、CDN 鏈接等。
如果你正在使用 Node.js 環(huán)境,可以通過 npm 來安裝 diff2html:
npm install diff2html
安裝后,你可以在你的項(xiàng)目中引入并使用它。
如果你只是想在 HTML 頁面中快速使用 diff2html,可以直接通過 CDN 鏈接引入:
<script src="https://cdn.jsdelivr.net/npm/diff2html/bundles/diff2html.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/diff2html.min.css" />
diff2html 的基本使用流程包括以下幾個(gè)步驟:
獲取 diff 數(shù)據(jù):首先,你需要有 diff 的數(shù)據(jù),這可以是通過 Git 命令生成的補(bǔ)丁文件內(nèi)容,也可以是兩個(gè)文件內(nèi)容直接通過 diff 命令生成的結(jié)果。
使用 diff2html 轉(zhuǎn)換:然后,使用 diff2html 提供的 API 或命令行工具(如果有的話)將 diff 數(shù)據(jù)轉(zhuǎn)換為 HTML。
顯示 HTML:最后,將轉(zhuǎn)換后的 HTML 插入到你的網(wǎng)頁中,或者使用 JavaScript 動(dòng)態(tài)創(chuàng)建 HTML 元素并添加到 DOM 中。
以下是一個(gè)簡(jiǎn)單的示例,演示如何使用 JavaScript 和 diff2html 將 diff 數(shù)據(jù)轉(zhuǎn)換為 HTML 并顯示在頁面上:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Diff2HTML Example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/diff2html.min.css" />
</head>
<body>
<div id="diff-output"></div>
<script src="https://cdn.jsdelivr.net/npm/diff2html/bundles/diff2html.min.js"></script>
<script>
// 假設(shè)這是你的 diff 數(shù)據(jù)
var diff = `diff --git a/file1.txt b/file1.txt
index 4d7a2fe..e43a40e 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,4 @@
Hello
World
+New Line
This is a test`;
// 使用 diff2html 轉(zhuǎn)換 diff 數(shù)據(jù)
var html = Diff2Html.getHtmlDiff(diff);
// 將轉(zhuǎn)換后的 HTML 插入到頁面中
document.getElementById('diff-output').innerHTML = html;
</script>
</body>
</html>
在這個(gè)示例中,我們首先通過 CDN 引入了 diff2html 的 JavaScript 和 CSS 文件。然后,在 <script> 標(biāo)簽中,我們定義了一個(gè)包含 diff 數(shù)據(jù)的字符串,并使用 Diff2Html.getHtmlDiff() 方法將其轉(zhuǎn)換為 HTML。最后,我們通過 JavaScript 將轉(zhuǎn)換后的 HTML 插入到頁面的 <div> 元素中。
diff2html 還提供了多種配置選項(xiàng)和樣式定制功能,允許你根據(jù)實(shí)際需求調(diào)整差異顯示的樣式和行為。例如,你可以設(shè)置不同的主題、自定義行號(hào)樣式、啟用/禁用文件標(biāo)題等。
要了解更多關(guān)于 diff2html 的高級(jí)配置和樣式選項(xiàng),建議查閱其官方文檔或源代碼。
diff2html 是一個(gè)功能強(qiáng)大的工具,它能夠?qū)?diff 輸出轉(zhuǎn)換為易于閱讀的 HTML 格式,極大地提升了代碼審查和版本對(duì)比的效率。無論是通過 npm 安裝還是通過 CDN 引入,
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。