【JAVA】iTEXT將html文檔轉PDF,spire.doc包html轉word(包括樣式修改和添加圖片/頁碼等設置)
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
項目需求使用velocity模板生成html內(nèi)容,將含有標題 / 段落 / 表格 / 圖片 的html文件轉化成PDF和word格式,并保留良好樣式。 iText起初只想實現(xiàn)word功能,但經(jīng)過嘗試和查詢一些例如poi等工具,都無法較好的保留原有的復雜樣式,后更換pdf需求進行嘗試,itext生成pdf已經(jīng)非常完美了。 spire.pdf / spire.doc / spire.office很好的第三方工具,能完美實現(xiàn)html轉pdf和word等格式文件,最關鍵的是有free版本,雖然有10頁的限制,結合需求頁可能夠用和付費版功能上無差別。 spire.pdf / spire.doc 冰藍科技 中文文檔 html轉pdf引入jar包引入itext倉庫<repository> <id>itext</id> <name>iText Repository - releases</name> <url>https://repo.itextsupport.com/releases</url> </repository> 在pom.xml引入maven(不包括spire.pdf)<!-- itextpdf --> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext-rtf</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> <!-- pdfHTML --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core</artifactId> <version>7.1.0</version> <type>pom</type> <exclusions><exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>html2pdf</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.0.8</version> </dependency> </dependencies> 可以下載spire.pdf.free以及各種實用jar包的maven倉庫maven倉庫(內(nèi)含spire.pdf.free和spire.doc.free) 生成pdfpackage com.sy.util;import com.itextpdf.html2pdf.ConverterProperties; import com.itextpdf.html2pdf.HtmlConverter; import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory; import com.itextpdf.html2pdf.css.media.MediaDeviceDescription; import com.itextpdf.html2pdf.css.media.MediaType; import com.itextpdf.kernel.pdf.*; import com.itextpdf.layout.font.FontProvider; import org.apache.commons.io.FileUtils; import org.apache.poi.util.IOUtils; import java.io.*; import static com.itextpdf.kernel.geom.PageSize.A4; import static com.itextpdf.kernel.geom.PageSize.A3;public class PdfService { //private static final Logger LOGGER = LoggerFactory.getLogger(PdffromHTML.class); public static final String FONT_NAME = "simsun.ttf"; public void copyFont(String targetDir,String fontPath) { FileOutputStream fout = null; FileInputStream fileInputStream = null; try { //InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(FONT_CLASS_PATH); fileInputStream = new FileInputStream(fontPath+"/WEB-INF/fonts/simsun.ttf"); File dir = new File(targetDir);FileUtils.forceMkdir(dir);fout = new FileOutputStream(targetDir + "simsun.ttf"); if (fileInputStream != null && fout != null) { org.apache.commons.io.IOUtils.copy(fileInputStream, fout); } } catch (Exception e) { //LOGGER.error("failed to copy font: ", e); e.printStackTrace(); } finally { if(null!=fileInputStream){ try {fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null!=fout){ try { fout.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void createPdffromHtml(String pdfFileName, InputStream htmlInputStream, String resourcePrefix) { PdfDocument pdfDoc = null; FileOutputStream outputStream = null; PdfWriter pdfWriter = null; try { outputStream = new FileOutputStream(resourcePrefix + pdfFileName); WriterProperties writerProperties = new WriterProperties(); writerProperties.addXmpMetadata(); pdfWriter = new PdfWriter(outputStream, writerProperties); pdfDoc = createPdfDoc(pdfWriter); PdfDocument pdfDocument = new PdfDocument(pdfWriter); pdfDocument.setDefaultPageSize(A3); //轉換器 ConverterProperties props = createConverterProperties(resourcePrefix); props.setBaseUri(resourcePrefix); //設置媒體的描述方式 MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN); props.setMediaDeviceDescription(mediaDeviceDescription); HtmlConverter.convertToPdf(htmlInputStream,pdfDocument, props); pdfDocument.close(); } catch (Exception e) { //LOGGER.error("failed to create pdf from html exception: ", e); e.printStackTrace(); } finally { try { outputStream.close(); pdfWriter.close(); } catch (IOException e) { e.printStackTrace(); } finally { outputStream = null; pdfWriter = null; } IOUtils.closeQuietly(pdfDoc); } } private PdfDocument createPdfDoc(PdfWriter pdfWriter) { PdfDocument pdfDoc; pdfDoc = new PdfDocument(pdfWriter); pdfDoc.getCatalog().setLang(new PdfString("zh-CN")); pdfDoc.setTagged(); pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true)); return pdfDoc; } private ConverterProperties createConverterProperties(String resourcePrefix) { ConverterProperties props = new ConverterProperties(); props.setFontProvider(createFontProvider(resourcePrefix)); props.setBaseUri(resourcePrefix); props.setCharset("UTF-8"); DefaultTagWorkerFactory tagWorkerFactory = new DefaultTagWorkerFactory(); props.setTagWorkerFactory(tagWorkerFactory); return props; } private FontProvider createFontProvider(String resourcePrefix) { FontProvider fp = new FontProvider(); fp.addStandardPdfFonts(); fp.addDirectory(resourcePrefix);return fp; } } 下面為main方法//pdf文件加載和生成統(tǒng)一路徑String pdfPath = localFilePath + name + ".pdf";File temFile = new File(localFilePath);if (!temFile.exists()) {temFile.mkdirs();}// 添加字體,這一步很關鍵,否則中文會顯示不出來PdfService pdfService = new PdfService();File file = new File(localFilePath + PdfService.FONT_NAME);if (!file.exists()) {//加載字體庫pdfService.copyFont(localFilePath,realPath);}//---------------------FileInputStream inputStream = new FileInputStream(url);File pdfFile = new File(pdfPath);//TODO html To PDFpdfService.createPdffromHtml(pdfFile.getName(), inputStream, localFilePath); pdf布局設置(以下內(nèi)容通過spire.doc/pdf包處理)package com.sy.util;import com.spire.doc.FileFormat; import com.spire.doc.documents.*; import com.spire.doc.fields.DocPicture; import com.spire.doc.fields.TextRange; import com.spire.pdf.*; import com.spire.doc.*; import com.spire.pdf.annotations.PdfRubberStampAnnotation; import com.spire.pdf.annotations.appearance.PdfAppearance; import com.spire.pdf.automaticfields.PdfCompositeField; import com.spire.pdf.automaticfields.PdfPageCountField; import com.spire.pdf.automaticfields.PdfPageNumberField; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.Dimension2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import com.spire.doc.FileFormat.*;public class PdfPageSetting { public static final int FLAG = 2; public void pdfPageSetting(String url,int flag) { //創(chuàng)建PdfDocument對象 PdfDocument originalDoc = new PdfDocument(); //加載PDF文件 originalDoc.loadfromFile(url); //第一頁插入空白頁(目的是刪除spirefree的內(nèi)置紅色警告語,主要是為了生成word使用,pdf可以用添加圖片方法進行遮蓋) originalDoc.getPages().insert(0); //創(chuàng)建一個新的PdfDocument實例 PdfDocument newDoc = new PdfDocument(); //遍歷所有PDF 頁面 Dimension2D dimension2D = new Dimension(); for (int i = 0; i < originalDoc.getPages().getCount(); i++) { PdfPageBase page = originalDoc.getPages().get(i); if (flag==1) { //設置新文檔頁面寬、高為原來的1倍 float scale = 1.0f; float width = (float) page.getSize().getWidth() * scale; float height = (float) page.getSize().getHeight() * scale; dimension2D.setSize(width, height); //設置新文檔第一頁的頁邊距為左右 PdfMargins margins = new PdfMargins(0, 20); PdfPageBase newPage = newDoc.getPages().add(dimension2D, margins); //復制原文檔的內(nèi)容到新文檔 newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float()); } if (flag==2) { //設置新文檔頁邊距為左右0、上下20(不能隨意更改,會影響主標題位置) PdfMargins margins = new PdfMargins(0,20,0,0); //設置新文檔頁面大小為A3 PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.A3, margins); //調(diào)整畫布,設置內(nèi)容也根據(jù)頁面的大小進行縮放 double wScale = (PdfPageSize.A3.getWidth() - 10) / PdfPageSize.A3.getWidth(); double hScale = (PdfPageSize.A3.getHeight() - 10) / PdfPageSize.A3.getHeight(); newPage.getCanvas().translateTransform(wScale, hScale); //復制原文檔的內(nèi)容到新文檔 newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float()); } if (flag==3) { //設置新文檔頁邊距 PdfMargins margins = new PdfMargins(0, 0); //設置新文檔頁面大小為A3, 頁面旋轉角度為0,紙張方向為水平 PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.A4, margins, PdfPageRotateAngle.Rotate_Angle_0, PdfPageOrientation.Landscape); //調(diào)整畫布,設置內(nèi)容也根據(jù)頁面的大小進行縮放 double wScale = PdfPageSize.A4.getHeight() / page.getSize().getWidth(); double hScale = PdfPageSize.A4.getWidth() / page.getSize().getHeight(); newPage.getCanvas().translateTransform(wScale, hScale); //復制原文檔的內(nèi)容到新文檔 newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float()); } } //保存 PDFnewDoc.saveToFile(url); newDoc.close(); originalDoc.close(); } pdf添加頁碼/*** 添加頁碼* @param targetPath*/public void addPageNum(String targetPath) {//加載PDF文檔PdfDocument doc = new PdfDocument();doc.loadfromFile(targetPath);//刪除第一頁空白頁doc.getPages().removeAt(0);//創(chuàng)建字體PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Default", Font.PLAIN, 10),true);//獲取頁面尺寸Dimension2D pageSize = doc.getPages().get(0).getSize();//初始化y坐標float y = (float) pageSize.getHeight() - 20;//遍歷文檔中的頁for (int i = 0; i < doc.getPages().getCount(); i++) {//初始化頁碼域PdfPageNumberField number = new PdfPageNumberField();//初始化總頁數(shù)域//PdfPageCountField count = new PdfPageCountField();//創(chuàng)建復合域PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(),"{0}", number);//設置復合域內(nèi)文字對齊方式compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));//測量文字大小Dimension2D textSize = font.measureString(compositeField.getText());//設置復合域的在PDF頁面上的位置及大小compositeField.setBounds(new Rectangle2D.Float(((float) pageSize.getWidth() - (float) textSize.getWidth())/2, y, (float) textSize.getWidth(), (float) textSize.getHeight()));//將復合域添加到PDF頁面compositeField.draw(doc.getPages().get(i).getCanvas());}//保存為另外一個文檔doc.saveToFile(targetPath);doc.close();} pdf添加圖片/*** 添加圖片到PDF* @param url pdf文件* @param baseUrl 項目路徑* @param savePath 文件保存路徑*/public static void addImgToPdf(String url,String baseUrl) {//創(chuàng)建PdfDocument對象PdfDocument doc = new PdfDocument();//加載PDF文檔doc.loadfromFile(url);//刪除第一頁空白頁//doc.getPages().removeAt(0);for (int i = 0; i < doc.getPages().getCount(); i++) {//獲取第i頁PdfPageBase page = doc.getPages().get(i);//加載圖片到PdfImage對象PdfImage image = PdfImage.fromFile(baseUrl + "img\\custom\\pdf.png");//獲取圖片高寬int width = image.getWidth();int height = image.getHeight();//創(chuàng)建PdfTemplate對象,大小跟圖片一致PdfTemplate template = new PdfTemplate(width, height);//在模板上繪制圖片template.getGraphics().drawImage(image, 0, 0, width, height);//創(chuàng)建PdfRubebrStampAnnotation對象,指定大小和位置Rectangle2D rect = new Rectangle2D.Float((float)(0), (float)(0), 835, 50);PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);//創(chuàng)建PdfAppearance對象PdfAppearance pdfAppearance = new PdfAppearance(stamp);//將模板應用為PdfAppearance的一般狀態(tài)pdfAppearance.setNormal(template);//將PdfAppearance 應用為圖章的樣式stamp.setAppearance(pdfAppearance);//添加圖章到PDFpage.getAnnotationsWidget().add(stamp);}//保存文檔doc.saveToFile(url);doc.close();} pdf轉word(windows/linux和tomcat下測試可用)//創(chuàng)建PdfDocument對象PdfDocument doc = new PdfDocument();//加載PDF文檔doc.loadfromFile(url);//保存文檔doc.saveToFile(url); html轉word//TODO html To DOCXDocument word = new Document();//html文件地址word.loadfromFile(url);word.saveToFile(wordPath, FileFormat.Docx);word.close();//TODO 添加頁眉和頁腳,設置word頁邊距(realPath為頁眉圖片地址)setUpPageOfWord(wordPath,realPath); word添加頁眉頁腳和頁碼/*** word添加頁眉* @param url* @param baseUrl* @param*/public static void setUpPageOfWord(String url,String baseUrl) {//創(chuàng)建Document對象Document doc = new Document();doc.loadfromFile(url);//獲取第一頁Section section = doc.getSections().get(0);Dimension2D dimension2D = new Dimension();dimension2D.setSize(800,1200);section.getPageSetup().setPageSize(dimension2D);//設置頁邊距section.getPageSetup().getMargins().setTop(40f);section.getPageSetup().getMargins().setBottom(40f);section.getPageSetup().getMargins().setLeft(60f);section.getPageSetup().getMargins().setRight(60f);//調(diào)用insertHeaderAndFooter方法插入頁眉頁腳到第一個sectioninsertHeaderAndFooter(section,baseUrl);//保存doc.saveToFile(url);doc.close();}private static void insertHeaderAndFooter(Section section,String baseUrl) {//分別獲取section的頁眉頁腳HeaderFooter header = section.getHeadersFooters().getHeader();HeaderFooter footer = section.getHeadersFooters().getFooter();//添加段落到頁眉Paragraph headerParagraph = header.addParagraph();//插入圖片到頁眉的段落DocPicture headerPicture = headerParagraph.appendPicture(baseUrl + "img/custom/word.png");headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Left);headerPicture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Bottom);//添加文字到頁眉的段落TextRange text = headerParagraph.appendText("上海煙草集團");//text.getCharacterFormat().setFontName("default");text.getCharacterFormat().setFontSize(10);text.getCharacterFormat().setItalic(true);headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);//設置文字環(huán)繞方式headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);//設置頁眉段落的底部邊線樣式headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);//添加段落到頁腳Paragraph footerParagraph = footer.addParagraph();//添加Field_Page和Field_Num_Pages域到頁腳段落,用于顯示當前頁碼和總頁數(shù)footerParagraph.appendField("page number", FieldType.Field_Page);footerParagraph.appendText(" of ");footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages);footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);//設置頁腳段落的頂部邊線樣式footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);} highcharts轉svg插入html模板/**將highcharts生成<img>的src標簽插入html中方便pdf轉化**/function img() {$(".highchartImg").css("display","none");$.each($(".highchartImg"),function (idx,c) {var charData = $(this).highcharts().getSVG();var canvas = document.getElementById("canvas");canvg(canvas,charData);var image = new Image();image.src = canvas.toDataURL("image/png");$(this).next().attr("src",image.src);})} 設置highcharts原始和導出圖片大小chart: {zoomType: 'xy', //圖片可拉伸type: 'column',width: 1200},exporting: {sourceWidth: 650,sourceHeight: 360,enabled: false, //屏蔽下載圖片選擇框} 總結:html的元素標簽一定要注意規(guī)范性,否則樣式會有偏差,不過慢慢調(diào)試也能弄出來,比如一些表頭的格式偏差,rowspan=“0” 這種就不要顯示在標簽里了,默認就好。 該文章在 2023/5/15 12:22:22 編輯過 |
關鍵字查詢
相關文章
正在查詢... |