Java教程

java url转pdf的两种方式

本文主要是介绍java url转pdf的两种方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

其一、单线程 可以解析一些极度不规范的代码

需要插件
链接:https://pan.baidu.com/s/1nbsku-1IachKVehIUmc3Fg
提取码:o7qc

<dependencies>
	<dependency>
		<groupId> e-iceblue </groupId>
		<artifactId>spire.pdf</artifactId>
		<version>3.11.6</version>
	</dependency>
</dependencies>

<repositories>
	<repository>
		<id>com.e-iceblue</id>
		<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
	</repository>
</repositories>
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;

//网页转pdf
public class Dome03 {
    public static void main(String[] args) {
        //定义需要转换的HTML
        String url = "https://www.baidu.com";

        //转换后的结果文档
        String fileName = "D:\\xiaoma\\test\\1.pdf";

        //解压后的插件本地地址
        String pluginPath = "D:\\xiaoma\\plugins-windows-x64";
        HtmlConverter.setPluginPath(pluginPath);

        //调用方法转换到PDF并设置PDF尺寸
        HtmlConverter.convert(url, fileName, true, 10000, new Size(700f, 800f), new PdfMargins(0));
    }
}

其二、支持并发 效率高

需要插件
链接:https://pan.baidu.com/s/150Bc0yEGTD81V0ZRA1Svvw
提取码:qeij

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class HtmlToPdfInterceptor extends Thread {
    private InputStream is;

    public HtmlToPdfInterceptor(InputStream is){
        this.is = is;
    }

    public void run(){
        try{
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line.toString()); //输出内容 可注释
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
public class HtmlToPdf {
    // wkhtmltopdf在系统中的路径
    private static final String toPdfTool = "D:\\xiaoma\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";

    /**
     * html转pdf
     *
     * @param srcPath
     *            html路径,可以是硬盘上的路径,也可以是网络路径
     * @param destPath
     *            pdf保存路径
     * @return 转换成功返回true
     */
    public static boolean convert(String srcPath, String destPath) {
        File file = new File(destPath);
        File parent = file.getParentFile();
        // 如果pdf保存路径不存在,则创建路径
        if (!parent.exists()) {
            parent.mkdirs();
        }
        StringBuilder cmd = new StringBuilder();
        if (System.getProperty("os.name").indexOf("Windows") == -1) {
            // 非windows 系统
            //toPdfTool = FileUtil.convertSystemFilePath("/home/ubuntu/wkhtmltox/bin/wkhtmltopdf");
        }
        cmd.append(toPdfTool);
        cmd.append(" ");
        cmd.append(" --disable-javascript ");
        cmd.append(" --encoding <UTF-8> ");
        cmd.append(srcPath);
        cmd.append(" ");
        cmd.append(destPath);

        boolean result = true;
        try {
            Process proc = Runtime.getRuntime().exec(cmd.toString());
            HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
            HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
            error.start();
            output.start();
            proc.waitFor();
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

}

这篇关于java url转pdf的两种方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!