整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

如何将网页生成PDF?仅需一步,轻松转换!

保存在线文章以便日后查阅,如何实现?想将网页内容转为PDF保存,如何转换?

今天,小福教大家如何将网页生成PDF文件,跟着小福一起来看看吧!

首先,我们打开福昕高级PDF编辑器,选择左上角“文件”>“创建”>“从网页”,或点击菜单栏“转换”>“从网页”;


在弹出的对话框中,粘贴转换的网页的URL链接,点击“创建”,福昕高级PDF编辑器将开始加载网页内容;


加载完毕后,网页就被转为PDF啦!此时,我们就可以对该页面进行进一步的编辑和调整,如文字提取、编辑、添加注释、划重点等。完成编辑后,点击保存即可。

此外,在转换时,我们可以点击“设置”来选择更多转换选项:

在弹出的“网页转换设置”对话框中,有“常规”和“页面布局”选项卡。常规选项卡可以设置网页的转换等级和转换内容,页面布局则可设置生成的PDF的页面尺寸、页边距和方向,以及多媒体类型及缩放比例。

这里给大家说说常用的一些设置:

在“常规”选项卡中:

1、转换等级:可以指定网站中要转换的网页等级,或勾选“转换整个站点”。

转换同一路径的网页:转换隶属于您输入的网页地址的所有网页;转换同一服务器上的网页:转换储存在同一服务器上的所有网页。

2、PDF设置:设置加载整个页面的超时时间。超过此设置时间,加载进程将自动中止,且仅已加载内容会被转换成PDF。默认值为120秒。

  • 在所生成的PDF文件中禁用所有超链接:勾选该项后,转换生成的PDF中的所有超链接均为无效链接。
  • 将所有的页面内容转换到同一个PDF页面:勾选该项后,所有相关内容将被转换到同一个PDF页面中。
  • 创建书签:勾选该项,转换后将自动生成被转换页面的书签。书签名为网页的域名。当网页域名相同时,将自动创建子书签并以页面的标题作为子书签的名称。若页面没有标题,则使用对应的URL作为子书签的名称。
  • 创建 PDF 标签:勾选该项以创建包含标签和其他辅助信息的PDF文档。
  • 在新建页面放置页眉和页脚:在生成的 PDF 的每一页上放置页眉和页脚。页眉显示您创建PDF的日期和网页标题。页脚显示网页URL(或 HTML 文件的文件路径)和页码。
  • 背景图像:勾选该项以保留页面背景,生成的 PDF 看起来将与原始网页完全一样。有时,网页上的背景可能会影响文本的显示效果,您可以取消勾选该项,这样转换后的页面可能与网页浏览器中显示的不同,但更易于阅读。

3、其他设置:

  • 转换图像:如需在 PDF 转换时转换图像,请勾选此项;反之则取消勾选。
  • 为链接添加下划线:如需在生成的 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转换过程。

iff2HTML:将Diff输出转换为HTML的便捷工具

github: https://github.com/rtfpessoa/diff2html

项目地址:https://diff2html.xyz/

在软件开发和版本控制过程中,diff 命令是不可或缺的,它用于显示文件之间的差异。然而,直接在命令行中查看 diff 的输出可能不够直观,特别是对于大型文件或复杂的代码更改。为了更友好地展示这些差异,diff2html 应运而生,它将 diff 的输出转换成易于阅读的 HTML 格式,使得代码审查、版本对比等任务变得更加高效。



一、diff2html 简介

diff2html 是一个轻量级的 JavaScript 库,它可以将 diff(通常是 Git 或其他版本控制系统生成的补丁文件)转换成漂亮的 HTML 格式。这个库不仅支持基本的文本差异显示,还提供了多种样式和配置选项,以满足不同场景下的需求。

二、安装与引入

diff2html 可以通过多种方式安装和使用,包括但不限于 npm、CDN 链接等。

1. 使用 npm 安装

如果你正在使用 Node.js 环境,可以通过 npm 来安装 diff2html

npm install diff2html

安装后,你可以在你的项目中引入并使用它。

2. 通过 CDN 引入

如果你只是想在 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 的基本使用流程包括以下几个步骤:

获取 diff 数据:首先,你需要有 diff 的数据,这可以是通过 Git 命令生成的补丁文件内容,也可以是两个文件内容直接通过 diff 命令生成的结果。

使用 diff2html 转换:然后,使用 diff2html 提供的 API 或命令行工具(如果有的话)将 diff 数据转换为 HTML。

显示 HTML:最后,将转换后的 HTML 插入到你的网页中,或者使用 JavaScript 动态创建 HTML 元素并添加到 DOM 中。

示例:使用 JavaScript 转换并显示 diff

以下是一个简单的示例,演示如何使用 JavaScript 和 diff2htmldiff 数据转换为 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>
// 假设这是你的 diff 数据
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 转换 diff 数据
var html = Diff2Html.getHtmlDiff(diff);

// 将转换后的 HTML 插入到页面中
document.getElementById('diff-output').innerHTML = html;
</script>
</body>
</html>

在这个示例中,我们首先通过 CDN 引入了 diff2html 的 JavaScript 和 CSS 文件。然后,在 <script> 标签中,我们定义了一个包含 diff 数据的字符串,并使用 Diff2Html.getHtmlDiff() 方法将其转换为 HTML。最后,我们通过 JavaScript 将转换后的 HTML 插入到页面的 <div> 元素中。

四、高级配置与样式

diff2html 还提供了多种配置选项和样式定制功能,允许你根据实际需求调整差异显示的样式和行为。例如,你可以设置不同的主题、自定义行号样式、启用/禁用文件标题等。

要了解更多关于 diff2html 的高级配置和样式选项,建议查阅其官方文档或源代码。

五、总结

diff2html 是一个功能强大的工具,它能够将 diff 输出转换为易于阅读的 HTML 格式,极大地提升了代码审查和版本对比的效率。无论是通过 npm 安装还是通过 CDN 引入,