liuyc 2 anni fa
parent
commit
b0b7b17f51

+ 0 - 221
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/HtmlToPdf.java

@@ -1,221 +0,0 @@
-package org.springblade.manager.utils;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-
-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.Rectangle;
-import com.itextpdf.kernel.pdf.PdfWriter;
-import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
-import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
-import org.jsoup.Jsoup;
-import org.jsoup.nodes.Element;
-import org.jsoup.select.Elements;
-import org.springblade.core.tool.utils.IoUtil;
-import org.springblade.core.tool.utils.ResourceUtil;
-
-/**
- * 利用HTML代码片段生成PDF
- *
- * zkh 2018年3月13日 下午2:05:17
- */
-public class HtmlToPdf {
-	private final static String DEST = "/Users/hongchuangyanfa/Desktop/pdf/test.pdf";
-	private final static String SRC = "/Users/hongchuangyanfa/Desktop/privateUrl/1550366339149856768.html";
-	public static final String FONT = "/Users/hongchuangyanfa/tool/NotoSansCJKsc-Regular.otf";
-
-	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 = 595;
-		float html_max_heght = 1132 ;
-/*		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;}");
-		}*/
-
-		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));
-		org.jsoup.nodes.Document doc = Jsoup.parse(htmlString);
-		//解析
-		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 = trs.size() * 36;
-		for(int i = 0 ; i <col.size();i++){
-			Element element = col.get(i);
-			html_max_width += Integer.parseInt(element.attr("width").replaceAll("px",""));
-		}
-
-		ConverterProperties props = new ConverterProperties();
-		DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
-		defaultFontProvider.addFont(FONT);
-		props.setFontProvider(defaultFontProvider);
-		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, html_max_heght));
-		Document document = HtmlConverter.convertToDocument(new FileInputStream(html), pdf, props);
-
-		HtmlConverter.
-		// 将所有内容在一个页面显示
-		EndPosition endPosition = new EndPosition();
-		LineSeparator separator = new LineSeparator(endPosition);
-		document.add(separator);
-		document.getRenderer().close();
-		PdfPage page = pdf.getPage(1);
-		float y = endPosition.getY() - 36;
-		page.setMediaBox(new Rectangle(0, 0, html_max_width, html_max_heght-y));
-		document.close();
-		pdf.close();
-	}*/
-
-	public static void main222(String[] args) throws IOException, InterruptedException {
-		HtmlToPdf html = new HtmlToPdf();
-
-		// shu
-		File file1 = ResourceUtil.getFile("/Users/hongchuangyanfa/Desktop/privateUrl/1560108472350015488.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,pdfPath);
-
-		//hen
-		//html.tomPdf("/Users/hongchuangyanfa/Desktop/pdfHtml/1556603810330181634.html","/Users/hongchuangyanfa/Desktop/pdfHtml/1556911621165547526222.pdf");
-	}
-
-	class EndPosition implements ILineDrawer {
-
-		/** A Y-position. */
-		protected float y;
-
-		/**
-		 * Gets the Y-position.
-		 *
-		 * @return the Y-position
-		 */
-		public float getY() {
-			return y;
-		}
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see
-		 * com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#draw(com.itextpdf.kernel.pdf.
-		 * canvas.PdfCanvas, com.itextpdf.kernel.geom.Rectangle)
-		 */
-		@Override
-		public void draw(PdfCanvas pdfCanvas, Rectangle rect) {
-			this.y = rect.getY();
-		}
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#getColor()
-		 */
-		@Override
-		public Color getColor() {
-			return null;
-		}
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#getLineWidth()
-		 */
-		@Override
-		public float getLineWidth() {
-			return 0;
-		}
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see
-		 * com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#setColor(com.itextpdf.kernel.
-		 * color.Color)
-		 */
-		@Override
-		public void setColor(Color color) {
-		}
-
-		/*
-		 * (non-Javadoc)
-		 * 
-		 * @see com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#setLineWidth(float)
-		 */
-		@Override
-		public void setLineWidth(float lineWidth) {
-		}
-
-	}
-
-}