java url转pdf的两种方式

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

需要插件
链接:https://pan.baidu.com/s/1UjLJOVlwKH2hyd9o94OFLQ?pwd=pbzh

<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 Demo {
    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/1xEBz8l1xf_nrm31k7si4Yg?pwd=ibw3

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 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;
    }

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
要将Java中的URL换为PDF,可以使用iText库。首先,您需要使用JavaURL类来打开并读取URL中的内容。然后,您可以使用iText库中的PdfWriter类将URL内容写入PDF文件中。以下是一个简单的示例代码: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class URLtoPDFConverter { public static void main(String[] args) { try { // 创建一个Document对象 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 读取URL内容并添加到PDF文档中 URL url = new URL("http://www.example.com/content"); InputStream is = url.openStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { document.add(new Paragraph(new String(buffer, 0, bytesRead))); } document.close(); is.close(); System.out.println("PDF文件已生成"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们首先创建一个Document对象,然后使用PdfWriter将其实例化,并指定输出的PDF文件名。接着,我们使用URL类打开和读取指定URL的内容,并将其添加到PDF文档中。最后,我们关闭文档并打印出一个成功的消息。 使用iText库能够很方便地实现将Java中的URL换为PDF文件,同时也能够对生成的PDF文件进行更多的定制和处理。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值