import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; import org.jsoup.Jsoup; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.*; import java.nio.charset.Charset; public class dd { private static final String PDF_PATH = "d:\\html2PDF.pdf"; private static final String HTML_PATH = "D:\\tootls\\ideaui\\ideawork\\ffcs\\code_1.4.7\\static\\ratingReport\\pdf\\sdf.html"; private static final String HTML_PATH_LOD = "d:\\old.html"; public static void main(String[] args) throws Exception { // BaseFont baseFont = BaseFont.createFont("E:\\testFile\\msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 文字如果要加粗,修改第三个参数 // Font msya = new Font(baseFont, 12, Font.NORMAL); // ======= // File f = new File(HTML_PATH); // org.jsoup.nodes.Document jsdocument = Jsoup.parse(f, "UTF-8"); // String html = jsdocument.html(); // Elements elements = jsdocument.getElementsByTag("table"); // for (Element d:elements){ // if (d.getElementsByTag("tbody").text().isEmpty()){ // d.remove(); // } // } // // FileWriter fileWriter = new FileWriter(HTML_PATH_LOD); // System.out.println(html); // fileWriter.write(html); // fileWriter.flush(); // fileWriter.close(); // File file1 = new File(HTML_PATH); // FileOutputStream fos = new FileOutputStream(file1); // BufferedWriter bw = null; // bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8")); // bw.write(html); // bw.close(); // fos.close(); // =========== // Document document = new Document(PageSize.A4, 20, 20, 20, 20);//左右上下的margin // document.setMargins(20, 20, 20, 20); // PdfWriter pdfwriter; // pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(PDF_PATH)); // document.open(); // XMLWorkerHelper wh = XMLWorkerHelper.getInstance(); // wh.parseXHtml(pdfwriter, document, new FileInputStream(HTML_PATH), Charset.forName("UTF-8"), new DefineFontProvider()); /* // 读取完 html 的内容后,可以继续往 pdf 文件中写其他内容 // 读取一个图片 Image image = Image.getInstance("E:\\testFile\\dream.jpg"); image.scalePercent(50); image.setAlignment(Image.ALIGN_RIGHT); // 设置图片的绝对位置 // image.setAbsolutePosition(0, 0); // image.scaleAbsolute(500, 400); // 插入一个图片 document.add(image); document.add(new Phrase("短语标题", msya)); // document.add(new Paragraph("\n")); document.add(new Paragraph("段落内容", msya)); */ // document.close(); // System.out.println("succeed"); } }


Jacko01 LV8
3月20日
liuyinwan
1月14日
暂无贡献等级
源蝈蝈
2024年12月28日
暂无贡献等级
3334004690 LV10
2024年6月24日
19950118
2024年4月19日
暂无贡献等级
zhos0212 LV19
2024年3月29日
467277 LV13
2024年3月26日
studengaaa
2024年3月17日
暂无贡献等级
a1325922140 LV1
2024年2月19日
wbbhappy LV13
2024年1月13日