Java使用Spire将网页保存为PDF并去除Evaluation Warning水印方案

 1.下载所需文件(45条消息) Java使用Spire讲网页保存为PDF并去除EvaluationWarning水印方案-Java文档类资源-CSDN文库

Java使用Spire将网页保存为PDF并去除Evaluation Warning水印方案_第1张图片

 2.加载到java项目

Java使用Spire将网页保存为PDF并去除Evaluation Warning水印方案_第2张图片

3.java例子,PdfDocument 可以save流

package com.export.excel.main;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import lombok.SneakyThrows;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class HtmlToPDF {

    public static void main(String[] args) {
        //spirePdf();
        spireRemove();
    }

    @SneakyThrows
    public static void spirePdf() {
        //定义需要转换的HTML 第一页必须定义一个空白页或者无效页
        String url = "http://localhost:63342/export-excel-spring/pdfTradeYiMiaoTwo.html?_ijt=2nt8n16s7knjvaqe1f624pcejo";

        //转换后的结果文档(结果文档保存在Java项目程序文件下)
        String fileName = "HtmlToPDF123.pdf";

        //解压后的插件本地地址(这里是把插件包放在了Java项目文件夹下,也可以自定义其他本地路径)
        String pluginPath = "D:\\IdeaProjects\\export-excel-spring\\plugins-windows-x64";
        HtmlConverter.setPluginPath(pluginPath);
        OutputStream outputStream = new FileOutputStream(fileName);
        //调用方法转换到PDF并设置PDF尺寸
        HtmlConverter.convert(url,outputStream);
    }

    public static void spireRemove() {
        //加载PDF文档
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("D:\\IdeaProjects\\export-excel-spring\\HtmlToPDF123.pdf");
        //添加一个空白页,目的为了删除jar包添加的水印,后面再移除这一页
        pdf.getPages().add();
        //移除第一个页
        pdf.getPages().remove(pdf.getPages().get(0));
        pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));
        //保存为另外一个文档
        pdf.saveToFile("D:\\IdeaProjects\\export-excel-spring\\HtmlToPDF1.pdf");
        System.out.println("添加成功");
    }
}

save流

package com.export.excel.main;

import com.export.excel.utils.TmpFileUtil;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class HtmlToPDF {

    public static void main(String[] args) {
        spirePdf();
    }

    @SneakyThrows
    public static void spirePdf() {
        //定义需要转换的HTML 第一页必须定义一个空白页或者无效页
        String url = "http://localhost:63342/export-excel-spring/pdfTradeYiMiaoTwo.html?_ijt=8b4juqcu901l29s9r4ccr48e8v";

        //转换后的结果文档(结果文档保存在Java项目程序文件下)
        //String fileName = "HtmlToPDF123.pdf";
        File tmpFile = TmpFileUtil.create(".pdf");
        //解压后的插件本地地址(这里是把插件包放在了Java项目文件夹下,也可以自定义其他本地路径)
        String pluginPath = "D:\\IdeaProjects\\export-excel-spring\\plugins-windows-x64";
        HtmlConverter.setPluginPath(pluginPath);
        OutputStream outputStream = new FileOutputStream(tmpFile);
        //调用方法转换到PDF并设置PDF尺寸
        HtmlConverter.convert(url,outputStream);
        //加载PDF文档
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile(tmpFile.toPath().toString());
        //添加一个空白页,目的为了删除jar包添加的水印,后面再移除这一页
        pdf.getPages().add();
        //移除第一个页和最后一页
        pdf.getPages().remove(pdf.getPages().get(0));
        pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));
        //pdf.saveToFile("D:\\IdeaProjects\\export-excel-spring\\HtmlToPDF1.pdf");
        pdf.saveToStream(outputStream);
        System.out.println("添加成功"+tmpFile.toPath());
        outputStream.close();
    }
}

临时文件工具类

package com.export.excel.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.UUID;

/**
 * 临时文件
 * 创建的文件会存放到操作系统的临时目录,操作系统会定时清理
 *
 */
@Slf4j
public class TmpFileUtil {

    /**
     * 创建临时文件
     * 将checkedException转为runtimeException
     *
     * @param fileExt 文件扩展名,比如 .doc, .jpg
     * @return
     */
    public static File create(String fileExt) {
        try {
            // 读取fileExt的后缀
            fileExt = fileExt.substring(fileExt.lastIndexOf("."));
            return File.createTempFile(UUID.randomUUID().toString(), fileExt);
        } catch (Exception e) {
            log.error("创建临时文件失败。", e);
            throw new RuntimeException("创建临时文件失败", e);
        }
    }

    /**
     * 创建临时文件
     *
     * @return
     */
    public static File create(String prefix, String suffix) {
        try {
            return File.createTempFile(prefix, suffix);
        } catch (Exception e) {
            log.error("创建临时文件失败。", e);
            throw new RuntimeException("创建临时文件失败", e);
        }
    }

}

增加无效页

Java使用Spire将网页保存为PDF并去除Evaluation Warning水印方案_第3张图片

 效果

Java使用Spire将网页保存为PDF并去除Evaluation Warning水印方案_第4张图片

 

你可能感兴趣的:(java,开发语言,后端)