整合营销服务商

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

免费咨询热线:

1-LINUX HTML 转 PDF-prince

1-LINUX HTML 转 PDF-prince

rincexml

It’s quick and simple to convert HTML to PDF with Prince. HTML is seamlessly transformed into documents you can print

下载地址

Prince - Download Prince

包装库

  • java
  • C#
  • PHP
  • Node
  • Ruby

适配情况

系统名称

x86_64

arm64

说明

Windows

-


Red Hat Enterprise Linux 7-9

-


Ubuntu 18-22


Debian GNU Linux 9-10

-


Debian GNU Linux 11-12


OpenSUSE

-


MacOS


Alpine Linux

-


Generic Linux


Free BSD

-


主要依赖

        <dependency>
            <groupId>com.princexml</groupId>
            <artifactId>prince-java-wrapper</artifactId>
            <version>1.3.0</version>
        </dependency>

测试代码

        // 获取 java 版本
        String version=System.getProperty("java.specification.version");

        // 获取系统类型
        String platform=System.getProperty("os.name", "");
        platform=platform.toLowerCase().contains("window") ? "win" : "linux";

        // 当前程序目录
        String current=System.getProperty("user.dir");

        System.out.println(String.format("current=%s", current));

        // html 文件路径
        File index=Paths.get(current, "..", "index.html").toFile();
        if (!index.exists()) {
            System.out.println(String.format("file not exist,file=%s", index.getAbsolutePath()));
            return;
        }
        String command=Paths.get(current, "..", "prince-15.2-win64", "bin", "prince.exe").toString();
        if (OSInfo.getOSType()==OSInfo.OSType.LINUX) {
            command="prince";
        }
        Prince prince=new Prince(command);
        // prince.setLog("/path/to/log.txt");
        // prince.addStyleSheet("/path/to/stylesheet.css");
        // prince.addScript("/path/to/script.js");
        prince.setJavaScript(true);

        try {
            // 转换 html 文件
            File file=Paths.get(current, String.format("java%s_%s.pdf", version, platform)).toFile();
            prince.convert(index.getAbsolutePath(), file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }

效果

prince-demo/java1.8_win.pdf · yjihrp/linux-html2pdf-demo - Gitee.com

prince-demo/java11_linux.pdf · yjihrp/linux-html2pdf-demo - Gitee.com

下一篇 2-LINUX HTML 转 PDF-itext5、8

pire.Doc for Java 是一款专业的Java Word组件,开发人员使用它可以轻松地将Word文档创建、读取、编辑、转换和打印等功能集成到自己的Java应用程序中。

本文介绍使用Spire.Doc for Java将WPS文字文档转为PDF格式的方法。

Spire.Doc for Java官方最新版免费下载试用,历史版本下载,在线文档和帮助文件下载-慧都网

代码如下:

import com.spire.doc.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class WordToPDF{
    public static void main(String[] args)throws IOException {
        //通过流加载WPS文字文档
        FileInputStream inputStream=new FileInputStream(new File("input.wps"));
        Document document=new Document();
        document.loadFromStream(inputStream, FileFormat.Doc);

        //保存为PDF
        document.saveToFile("WPStoPDF.pdf",FileFormat.PDF);
    }
}

原WPS文字文档:

转换后的WPS文字文档:



Spire.Doc for Java 4.7.0已发布。该版本增强了转换Word/RTF到PDF,以及转换HTML到Word的功能。此外,本次更新还修复了加载和保存Word文档等时出现的问题。

DF是一种比较稳定的电子文档格式,而HTML则是一种通用的网页格式,如何才能实现这两种格式之间的互转呢?

首先将网页转成PDF是相当来说比较简单的操作,而且只要我们对PDF格式比较熟悉,一定都知道PDF虚拟打印机可以将各种格式打印并以PDF格式保存,所以此方法也同样适用于HTML转换PDF,具体操作方法就是先打开网页后,在网页任意处右击并选择“打印”

接着在左上角的打印设置页面,将目标打印机更改为电脑已有的PDF虚拟打印机或另存为PDF,再点击保存并选择文档保存位置后就可以了。

而将PDF文件转成网页就需要使用一些工具了,先下载并安装极速玩转后,选择PDF转换中的PDF转HTML进入转换页面,并添加需要转换的PDF文档。

然后在左下角的输出路径设置好转换后的HTML网页保存文件夹后,点击右下角的开始转换就可以了。