|
@@ -4,16 +4,12 @@ import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.itextpdf.html2pdf.ConverterProperties;
|
|
|
import com.itextpdf.html2pdf.HtmlConverter;
|
|
|
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
|
|
|
import com.itextpdf.kernel.colors.Color;
|
|
|
-import com.itextpdf.kernel.geom.PageSize;
|
|
|
import com.itextpdf.kernel.geom.Rectangle;
|
|
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
|
|
import com.itextpdf.kernel.pdf.PdfPage;
|
|
@@ -22,7 +18,6 @@ import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
|
|
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
|
|
|
import com.itextpdf.layout.Document;
|
|
|
import com.itextpdf.layout.element.LineSeparator;
|
|
|
-import com.itextpdf.text.RectangleReadOnly;
|
|
|
import org.jsoup.Jsoup;
|
|
|
import org.jsoup.nodes.Element;
|
|
|
import org.jsoup.select.Elements;
|
|
@@ -39,7 +34,59 @@ public class HtmlToPdf {
|
|
|
private final static String SRC = "/Users/hongchuangyanfa/Desktop/privateUrl/1550366339149856768.html";
|
|
|
public static final String FONT = "/Users/hongchuangyanfa/tool/NotoSansCJKsc-Regular.otf";
|
|
|
|
|
|
- public void tomPdf(String html, String DEST) throws FileNotFoundException, IOException {
|
|
|
+ public static void tomPdf(org.jsoup.nodes.Document doc, String DEST) throws FileNotFoundException, IOException {
|
|
|
+
|
|
|
+ Element style = doc.select("style").first();
|
|
|
+
|
|
|
+
|
|
|
+ Element table = doc.select("table").first();
|
|
|
+
|
|
|
+ Elements col = doc.select("Col");
|
|
|
+ Elements trs = table.select("tr");
|
|
|
+ float html_max_width = 0;
|
|
|
+ float html_max_heght = 0 ;
|
|
|
+ for(int i = 0 ; i <col.size();i++){
|
|
|
+ Element element = col.get(i);
|
|
|
+ html_max_width += Integer.parseInt(element.attr("width").replaceAll("px",""));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0 ;i<trs.size();i++){
|
|
|
+ Element tr = trs.get(i);
|
|
|
+ Elements tds = tr.select("td");
|
|
|
+ if(tds.size()>=1){
|
|
|
+ String heg[] =tds.get(0).attr("style").split(";");
|
|
|
+ for (String str:heg){
|
|
|
+ if(str.indexOf("height:")>=0){
|
|
|
+ html_max_heght += Integer.parseInt(str.split(":")[1].replaceAll("px",""));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println(html_max_heght+"_"+html_max_width);
|
|
|
+ if(html_max_heght>html_max_width){
|
|
|
+ html_max_heght+=250;
|
|
|
+ style.append(" @page{size:"+html_max_width+"px "+html_max_heght+"px} table{page-break-inside:avoid;}");
|
|
|
+ }else{
|
|
|
+ html_max_width+=100;
|
|
|
+ if(html_max_heght<-650){
|
|
|
+ html_max_heght = html_max_heght*2-400;
|
|
|
+ }else{
|
|
|
+ html_max_heght+=250;
|
|
|
+ }
|
|
|
+ style.append(" @page{size:"+html_max_width+"px "+html_max_heght+"px} table{page-break-inside:avoid;}");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ConverterProperties props = new ConverterProperties();
|
|
|
+ DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
|
|
|
+ defaultFontProvider.addFont(FONT);
|
|
|
+ props.setFontProvider(defaultFontProvider);
|
|
|
+ PdfWriter writer = new PdfWriter(DEST);
|
|
|
+ HtmlConverter.convertToPdf(doc.html(),writer,props);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+/* public void tomPdf(String html, String DEST) throws FileNotFoundException, IOException {
|
|
|
|
|
|
File file1 = ResourceUtil.getFile(html);
|
|
|
String htmlString = IoUtil.readToString(new FileInputStream(file1));
|
|
@@ -56,7 +103,6 @@ public class HtmlToPdf {
|
|
|
html_max_width += Integer.parseInt(element.attr("width").replaceAll("px",""));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
ConverterProperties props = new ConverterProperties();
|
|
|
DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
|
|
|
defaultFontProvider.addFont(FONT);
|
|
@@ -64,14 +110,18 @@ public class HtmlToPdf {
|
|
|
PdfWriter writer = new PdfWriter(DEST);
|
|
|
PdfDocument pdf = new PdfDocument(writer);
|
|
|
if(html_max_width>html_max_heght){
|
|
|
+ //html_max_width = 1123;
|
|
|
+ html_max_heght = 794;
|
|
|
html_max_width = 1123;
|
|
|
html_max_heght = 794;
|
|
|
}else{
|
|
|
html_max_width = 794;
|
|
|
html_max_heght = 1123;
|
|
|
}
|
|
|
- pdf.setDefaultPageSize(new PageSize(html_max_width-200, html_max_heght));
|
|
|
+ pdf.setDefaultPageSize(new PageSize(html_max_width, html_max_heght));
|
|
|
Document document = HtmlConverter.convertToDocument(new FileInputStream(html), pdf, props);
|
|
|
+
|
|
|
+ HtmlConverter.
|
|
|
// 将所有内容在一个页面显示
|
|
|
EndPosition endPosition = new EndPosition();
|
|
|
LineSeparator separator = new LineSeparator(endPosition);
|
|
@@ -79,12 +129,32 @@ public class HtmlToPdf {
|
|
|
document.getRenderer().close();
|
|
|
PdfPage page = pdf.getPage(1);
|
|
|
float y = endPosition.getY() - 36;
|
|
|
- page.setMediaBox(new Rectangle(0, y, html_max_width-200, html_max_heght));
|
|
|
+ page.setMediaBox(new Rectangle(0, 0, html_max_width, html_max_heght-y));
|
|
|
document.close();
|
|
|
pdf.close();
|
|
|
- }
|
|
|
+ }*/
|
|
|
+
|
|
|
+ public static void main22(String[] args) throws IOException, InterruptedException {
|
|
|
+ HtmlToPdf html = new HtmlToPdf();
|
|
|
+
|
|
|
+ // shu
|
|
|
+ File file1 = ResourceUtil.getFile("/Users/hongchuangyanfa/Desktop/privateUrl/1557303077508218880.html");
|
|
|
+ String htmlString = IoUtil.readToString(new FileInputStream(file1));
|
|
|
+ org.jsoup.nodes.Document doc = Jsoup.parse(htmlString);
|
|
|
|
|
|
|
|
|
+ String pdfPath= "/Users/hongchuangyanfa/Desktop/pdfHtml/1556911621165547526222xxx.pdf";
|
|
|
+ File tabpdf = ResourceUtil.getFile(pdfPath);
|
|
|
+ if(tabpdf.exists()){
|
|
|
+ tabpdf.delete();
|
|
|
+ }
|
|
|
+ Thread.sleep(200);
|
|
|
+ //html.tomPdf(doc.html(),pdfPath);
|
|
|
+
|
|
|
+ //hen
|
|
|
+ //html.tomPdf("/Users/hongchuangyanfa/Desktop/pdfHtml/1556603810330181634.html","/Users/hongchuangyanfa/Desktop/pdfHtml/1556911621165547526222.pdf");
|
|
|
+ }
|
|
|
+
|
|
|
class EndPosition implements ILineDrawer {
|
|
|
|
|
|
/** A Y-position. */
|