|
@@ -17,10 +17,17 @@
|
|
package org.springblade.manager.controller;
|
|
package org.springblade.manager.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
-import com.spire.pdf.PdfDocument;
|
|
|
|
|
|
+import com.itextpdf.html2pdf.ConverterProperties;
|
|
|
|
+import com.itextpdf.html2pdf.HtmlConverter;
|
|
|
|
+import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
|
|
|
|
+import com.itextpdf.io.font.FontProgram;
|
|
|
|
+import com.itextpdf.io.font.FontProgramFactory;
|
|
|
|
+import com.itextpdf.kernel.geom.PageSize;
|
|
|
|
+import com.itextpdf.kernel.pdf.PdfDocument;
|
|
|
|
+import com.itextpdf.kernel.pdf.PdfWriter;
|
|
|
|
+import com.itextpdf.layout.font.FontProvider;
|
|
import com.spire.pdf.PdfPageSettings;
|
|
import com.spire.pdf.PdfPageSettings;
|
|
import com.spire.pdf.graphics.PdfMargins;
|
|
import com.spire.pdf.graphics.PdfMargins;
|
|
-import com.spire.pdf.htmlconverter.qt.HtmlConverter;
|
|
|
|
import com.spire.xls.Workbook;
|
|
import com.spire.xls.Workbook;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
@@ -55,10 +62,7 @@ import org.springblade.manager.vo.LinkdataInfoVO;
|
|
import org.springblade.manager.service.ILinkdataInfoService;
|
|
import org.springblade.manager.service.ILinkdataInfoService;
|
|
import org.springblade.core.boot.ctrl.BladeController;
|
|
import org.springblade.core.boot.ctrl.BladeController;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.FileNotFoundException;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -222,41 +226,65 @@ public class LinkdataInfoController extends BladeController {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-/* public static void main(String[] args) throws FileNotFoundException {
|
|
|
|
-*//* String htmlUrl="/Users/hongchuangyanfa/Desktop/privateUrl/1549653308367437824.html";
|
|
|
|
- File file = ResourceUtil.getFile(htmlUrl);
|
|
|
|
- FileInputStream inputStream = new FileInputStream(file);
|
|
|
|
- Workbook wb = new Workbook();
|
|
|
|
- wb.loadFromMHtml(inputStream);
|
|
|
|
- String htmlUrl2="/Users/hongchuangyanfa/Desktop/privateUrl/1549653308367437824.pdf";
|
|
|
|
- wb.saveToFile(htmlUrl2);*//*
|
|
|
|
|
|
+ /*public static void main(String[] args) throws FileNotFoundException {
|
|
|
|
+ String fontPath ="/Users/hongchuangyanfa/tool/simhei.ttf";
|
|
|
|
+ ConverterProperties properties = creatBaseFont(fontPath);
|
|
|
|
|
|
- String url = "https://www.baidu.com/";
|
|
|
|
|
|
+ String pdfPath="/Users/hongchuangyanfa/Desktop/pdf//1234567.pdf";
|
|
|
|
|
|
- //转换后的结果文档(结果文档保存在Java项目程序文件下)
|
|
|
|
- String fileName = "/Users/hongchuangyanfa/Desktop/pdf/HtmlToPDF.pdf";
|
|
|
|
|
|
+ String htmlPath="/Users/hongchuangyanfa/Desktop/privateUrl/1550417957572575232.html";
|
|
|
|
+ String htmlPDFPath="/Users/hongchuangyanfa/Desktop/privateUrl/123456.html";
|
|
|
|
|
|
- //解压后的插件本地地址(这里是把插件包放在了Java项目文件夹下,也可以自定义其他本地路径)
|
|
|
|
- String pluginPath = "/Users/hongchuangyanfa/tool/plugins_mac/";
|
|
|
|
- HtmlConverter.setPluginPath(pluginPath);
|
|
|
|
-
|
|
|
|
- //调用方法转换到PDF并设置PDF尺寸
|
|
|
|
- HtmlConverter.convert(url, fileName, true, 1000, new Size(700f, 800f), new PdfMargins(0));
|
|
|
|
-
|
|
|
|
- System.out.println("完成");
|
|
|
|
|
|
+ File file1 = ResourceUtil.getFile(htmlPath);
|
|
|
|
+ String htmlString = IoUtil.readToString(new FileInputStream(file1));
|
|
|
|
+ // 样式集合
|
|
|
|
+ Document doc = Jsoup.parse(htmlString);
|
|
|
|
|
|
- *//* String url="/Users/hongchuangyanfa/Desktop/privateUrl/1550287371163926528.html";
|
|
|
|
- //指定输出文档路径
|
|
|
|
- String fileName = "/Users/hongchuangyanfa/Desktop/pdf/UrlToPdf.pdf";
|
|
|
|
- //指定插件路径
|
|
|
|
- String pluginPath = "/Users/hongchuangyanfa/tool/plugins_mac";
|
|
|
|
|
|
+ //解析
|
|
|
|
+ Element table = doc.select("table").first();
|
|
|
|
+ Elements trs = table.select("tr");
|
|
|
|
+ for(int i = 0 ;i <=trs.size()-1 ;i++) {
|
|
|
|
+ Element tr = trs.get(i);
|
|
|
|
+ Elements tds = tr.select("td");
|
|
|
|
+ for (int j = 0; j < tds.size(); j++) {
|
|
|
|
+ Element data = tds.get(j);
|
|
|
|
+ if(data.html().indexOf("keyname")>0){
|
|
|
|
+ data.empty().append("测试pdf");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ File pdffile = ResourceUtil.getFile(pdfPath);
|
|
|
|
+ PdfWriter writer = new PdfWriter(pdffile);
|
|
|
|
+ com.itextpdf.kernel.pdf.PdfDocument pdf = new PdfDocument(writer);
|
|
|
|
+ PageSize pageSize = new PageSize(795,842);
|
|
|
|
+ pdf.setDefaultPageSize(pageSize);
|
|
|
|
+ try {
|
|
|
|
+ HtmlConverter.convertToPdf(doc.html(),pdf,properties);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- //设置插件路径
|
|
|
|
- HtmlConverter.setPluginPath(pluginPath);
|
|
|
|
|
|
+*/
|
|
|
|
+ /**
|
|
|
|
+ * 设置BaseFont
|
|
|
|
+ * @param fontPath 字体路径
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private static ConverterProperties creatBaseFont(String fontPath) {
|
|
|
|
+ ConverterProperties properties = new ConverterProperties();
|
|
|
|
+ FontProvider fontProvider = new DefaultFontProvider();
|
|
|
|
+ FontProgram fontProgram;
|
|
|
|
+ try {
|
|
|
|
+ fontProgram = FontProgramFactory.createFont(fontPath);
|
|
|
|
+ fontProvider.addFont(fontProgram);
|
|
|
|
+ properties.setFontProvider(fontProvider);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ // log.error("creat base font erro" , e );
|
|
|
|
+ System.out.println("creat base font erro");
|
|
|
|
+ }
|
|
|
|
+ return properties;
|
|
|
|
+ }
|
|
|
|
|
|
- //将URL转换为PDF
|
|
|
|
- HtmlConverter.convert(url, fileName, true, 1000000, new Size(1200f, 1000f), new PdfMargins(0));
|
|
|
|
- System.out.println("ok");*//*
|
|
|
|
|
|
|
|
- }*/
|
|
|
|
}
|
|
}
|