List of usage examples for com.itextpdf.tool.xml.parser XMLParser parse
public void parse(final Reader reader) throws IOException
From source file:book.pdftemplates.FillTemplateHelper.java
public static ElementList parseHtml(String content, String style, TagProcessorFactory tagProcessors) throws IOException { // CSS/*w w w. j a v a 2 s .c om*/ CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(style)); cssResolver.addCss(cssFile); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(tagProcessors); htmlContext.autoBookmark(false); // Pipelines ElementList elements = new ElementList(); ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null); HtmlPipeline html = new HtmlPipeline(htmlContext, end); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new FileInputStream(content)); return elements; }
From source file:com.apcb.utils.utils.PdfCreator.java
public static String createPdfWStyles(String fileName, String innerHTML, Document document, String CSS, String imagesSourcesPath) throws IOException, DocumentException { log.info("Creando HTML nombre: " + fileName); File fileHTML = new File(fileName); fileHTML.getParentFile().mkdirs();//from ww w. j av a2s.c om PrintWriter writerHTML = new PrintWriter(fileHTML, "UTF-8"); writerHTML.println(innerHTML); writerHTML.flush(); writerHTML.close(); log.info("Creado HTML ruta: " + fileHTML.getAbsolutePath()); log.info("Creando PDF nombre: " + fileName + ".pdf"); File filePDF = new File(fileName + ".pdf"); //File file = new File(fileName); filePDF.getParentFile().mkdirs(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName + ".pdf")); document.open(); CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes())); cssResolver.addCss(cssFile); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); htmlContext.setImageProvider(new AbstractImageProvider() { @Override public String getImageRootPath() { return imagesSourcesPath; } }); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new ByteArrayInputStream(innerHTML.getBytes())); document.close(); log.info("Creado PDF ruta: " + filePDF.getAbsolutePath()); log.info("filePDF.getName() " + filePDF.getName()); return filePDF.getName(); }
From source file:com.propelics.pdfcreator.PdfcreatorModule.java
License:Open Source License
/** * @method generatePDFWithHTML//from w w w . ja v a 2 s . com * Generates a PDF with the given file name, based on a HTML file * @param {String} filename Name of the PDF file * @param {String} html String with the HTML * @param {String} [author] Author for metadata * Fires a "complete" event when the PDF is generated * Fires a "error" event when a error is presented */ @Kroll.method(runOnUiThread = true) public void generatePDFWithHTML(final HashMap args) { Log.d(PROXY_NAME, "generatePDFWithHTML()"); String html = ""; String author = ""; String filename = ""; final float marginPt = 28.35f;//1cm == 28.35pt try { if (args.containsKey("filename")) { filename = (String) args.get("filename"); Log.d(PROXY_NAME, "filename: " + filename); } else return; if (args.containsKey("html")) { html = (String) args.get("html"); Log.d(PROXY_NAME, "html: " + html); } else return; if (args.containsKey("author")) { author = (String) args.get("author"); Log.d(PROXY_NAME, "author: " + author); } //create a new document Document document = new Document(PageSize.LETTER, marginPt, marginPt, marginPt, 0); TiBaseFile file = TiFileFactory.createTitaniumFile(filename, true); // Parse to XHTML StringWriter xhtmlWriter = new StringWriter(); Tidy tidy = new Tidy(); // tidy.setXHTML(true); tidy.setXmlOut(true); tidy.parse(new StringReader(html), xhtmlWriter); String xhtml = xhtmlWriter.toString(); //get Instance of the PDFWriter PdfWriter pdfWriter = PdfWriter.getInstance(document, file.getOutputStream()); //document header attributes document.addAuthor(author); document.addCreationDate(); document.setPageSize(PageSize.LETTER); //open document document.open(); // From Stack Overflow lol MyFontFactory fontFactory = new MyFontFactory(); FontFactory.setFontImp(fontFactory); // HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl(fontFactory)); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, pdfWriter))); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(worker); p.parse(new StringReader(xhtml)); // Finish SO c&P // Older code /* // Font Provider creation MyFontFactory fontProvider = new MyFontFactory(); // fontProvider.register("/DroidSans.ttf"); //get the XMLWorkerHelper Instance XMLWorkerHelper worker = XMLWorkerHelper.getInstance(); //convert to PDF worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(xhtml.getBytes("UTF-8")), null, fontProvider); //Load xhtml */ //close the document document.close(); //close the writer pdfWriter.close(); sendCompleteEvent(filename); } catch (Exception e) { sendErrorEvent(e); } }
From source file:com.sarav.donormgmttool.SendMail.java
public void writePdf(OutputStream outputStream, float ramount, java.sql.Date rdate, int rnum, String rname, String rmode) throws Exception { Document document = new Document(); //step2 http://developers.itextpdf.com/examples/xml-worker-itext5/html-tables PdfWriter writer = PdfWriter.getInstance(document, outputStream); String PDFContent = "<center>\n" + " <table style=\"text-align:center\">\n" + " <tr>\n" + " <td>\n" + " <img src=\"Images/Small - Team Everest Logo.png\"> \n" + " </td>\n" + " <td>\n" + " <h1 style=\"font-family: Arial\"><b><u>Team Everest</u></b></h1><b>(Regd No: 2292/09)</b>\n" + " <p style=\"font-family: Times\"> 5/1B, Magaveerar Street, Arnipalayam, Arni 632301<br>\n" + " Tiruvannamalai Dt, Tamilnadu, India Phone: +91 89399 12365<br>\n" + " Email: info@teameverestindia.org Website: www.teameverestindia.org</p>\n" + " </td>\n" + " </tr>\n" + " <tr><td><br><br><br><br></td></tr>\n" + " <tr>\n" + " <td>\n" + " <b>Reciept No:</b>*********\n" + " </td>\n" + " <td>\n" + " \n" + " </td>\n" + " <td>\n" + " <b>Date:</b>********\n" + " </td>\n" + " </tr>\n" + " <tr><td><br><br><br><br></td></tr>\n" + " <tr>\n" + " <td></td>\n" + " <td>\n" + " <p style=\"font-family: Times\">Received the sum of Rs. <b>*****/- (*******)</b>, with thanks from\n" + " <b>*******</b> as donation for Team Everest.</p>\n" + " </td>\n" + " </tr>\n" + " <tr><td><br><br><br><br></td></tr>\n" + " <tr>\n" + " <td></td>\n" + " <td>\n" + " <p style=\"text-align:left\"><b>Amount:</b> Rs.******** <b>Mode of Donation:</b>********</p>\n" + " </td>\n" + " </tr>\n" + " <tr><td><br><br><br><br></td></tr>\n" + " <tr><td><br><br><br><br></td>\n" + " <td style=\"text-align:right\">\n" + " <p>For Team Everest </p>\n" + " <img src =\"Images/Signature.JPG\">\n" + " </td>\n" + " </tr>\n" + " <tr><td><br><br><br><br></td></tr>\n" + " <tr>\n" + " <td></td>\n" + " <td>\n" + " <p>*Donations are exempted under 80G of Income Tax Act<br>\n" + " 1961 C, No. DIT (E) No. 2 (19) 1011. Pan No: AABTT6850G.</p>\n" + " </td>\n" + " </tr>\n" + " </table>\n" + " </center>"; document.open();/* ww w. java 2s . c om*/ document.addTitle("EReceipt"); document.addSubject("EreceiptPDF"); document.addKeywords("iText, email"); document.addAuthor("AKSarav"); document.addCreator("AkSarav"); //HTMLWorker htmlworker = new HTMLWorker(document); StringBuilder contentBuilder = new StringBuilder(); Paragraph paragraph = new Paragraph(); //System.out.println("PDFCONTENT"+PDFContent); contentBuilder.append(PDFContent); //HTML Processor HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); //XMLWorker XMLWorker worker = new XMLWorker(html, true); XMLParser p = new XMLParser(worker); p.parse(new ByteArrayInputStream(contentBuilder.toString().getBytes())); // Closing the document document.close(); }
From source file:com.semfapp.adamdilger.semf.Pdf.java
License:Open Source License
private void createPDFNew(String filePath, String htmlString, @Nullable ArrayList<ImageFile> images) { File file = null;/* w w w. j a v a2 s . c om*/ try { file = new File(filePath); // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12.5f); // step 3 document.open(); // step 4 // CSS CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(activity.getAssets().open("styles.css")); cssResolver.addCss(cssFile); // HTML HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); // XML Worker XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); Drawable d = activity.getResources().getDrawable(R.drawable.logo_icon); Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapData = stream.toByteArray(); Image image = Image.getInstance(bitmapData); image.setAbsolutePosition(35, 763); image.scalePercent(12); document.add(image); p.parse(new ByteArrayInputStream(htmlString.getBytes(StandardCharsets.UTF_8))); if (images != null) { System.out.println("Adding IMage"); for (int x = 0; x < images.size(); x++) { Image cursor = images.get(x).getImage(); float ratio = cursor.getPlainHeight() / cursor.getPlainWidth(); float imgWidth = document.getPageSize().getWidth() - 100; float imgHeight = document.getPageSize().getHeight() - 100; cursor.scaleToFit(new Rectangle(imgWidth, imgHeight)); document.add(cursor); } } // step 5 document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:it.digitalhumanities.dhcpublisher.DHCPublisher.java
private void convertFile(final File subDir) throws DocumentException, IOException { File[] htmlFiles = subDir.listFiles(new FilenameFilter() { @Override// w w w . ja va 2s . co m public boolean accept(File dir, String name) { return name.endsWith(".html"); } }); if (htmlFiles.length > 0) { File htmlFile = htmlFiles[0]; String targetName = htmlFile.getName().substring(0, htmlFile.getName().length() - 4) + "pdf"; Document document = new Document(); File targetFile = new File(subDir, targetName); if (targetFile.exists()) { targetFile.delete(); } try (FileOutputStream fos = new FileOutputStream(targetFile)) { PdfWriter writer = PdfWriter.getInstance(document, fos); writer.getAcroForm().setNeedAppearances(true); document.open(); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); htmlContext.setImageProvider(new AbstractImageProvider() { public String getImageRootPath() { return subDir.getAbsolutePath(); } }); CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer))); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(worker); try (FileInputStream fis = new FileInputStream(htmlFile)) { p.parse(fis); } finally { document.close(); } } } else { throw new IllegalArgumentException(subDir + " does not contain HTML files!"); } }
From source file:org.freelancertech.poc.itext.ParseHtmlTable1.java
public static byte[] createPdf() throws DocumentException, IOException { try (ByteArrayOutputStream os = new ByteArrayOutputStream();) { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, os); document.open();//from www . ja va 2 s. c o m ClassLoader classLoader = ParseHtmlTable1.class.getClassLoader(); CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream(STYLE_FILE_NAME)); cssResolver.addCss(cssFile); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); InputStream is = classLoader.getResourceAsStream(HTML_FILE_NAME); p.parse(is); document.close(); return os.toByteArray(); } }
From source file:org.freelancertech.poc.itext.ParseHtmlTable1.java
public static byte[] createPdf(String content) throws DocumentException, IOException { try (ByteArrayOutputStream os = new ByteArrayOutputStream();) { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, os); document.open();/*from w w w .j av a 2 s . c o m*/ ClassLoader classLoader = ParseHtmlTable1.class.getClassLoader(); CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream(STYLE_FILE_NAME)); cssResolver.addCss(cssFile); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); InputStream is = new ByteArrayInputStream(content.getBytes()); p.parse(is); document.close(); return os.toByteArray(); } }
From source file:projekt.Class.SaveToPDF.java
/** * * Metoda, ktra tworzy plik pdf na podan ciek * * @param file cieka dostpu do pluku na ktrym ma by zapisaby plik pdf * @throws IOException wyjtek wejcia/ wyjcia * @throws DocumentException wyjatek podczas tworzenia dokumentu *//*from w w w.j a v a 2 s . co m*/ public static void createPdf(File file) throws IOException, DocumentException { Document document = new Document(); String HTML = "src/projekt/HTML/Diagnoza/diagnoza.html"; String CSS = "src/projekt/HTML/Diagnoza/styl.css"; PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setInitialLeading(12.5f); document.open(); CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS)); cssResolver.addCss(cssFile); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new FileInputStream(HTML)); document.close(); }
From source file:scoretracker.beans.EJB.PDFService.java
public void createPdfStudent(Student s) throws IOException, DocumentException { //The directory in which the PDF will be stord, including a name final String PDFLOCATION = "C:\\created_PDFs\\" + s.getName() + s.getPrename() + "_" + s.getRNr() + "_" + s.getClassId().getName() + ".pdf"; //Collect student's points List<Teststudent> tests = dataservice.getDataPPSTS(s); File file = new File(PDFLOCATION); //Creating the required directory structure file.getParentFile().mkdirs();//from ww w . ja va 2 s. c o m Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); //Create the table headers for our table StringBuilder sb = new StringBuilder(); sb.append("<h4>"); sb.append("Overview of all tests made by: "); sb.append(s.getName()); sb.append(" "); sb.append(s.getPrename()); sb.append("</h4>"); sb.append("<p>Class: "); sb.append(s.getClassId().getName()); sb.append("</p><br/>"); sb.append("<table border=\"2\">"); sb.append("<tr>"); sb.append("<th>Course</th>"); sb.append("<th>Score</th>"); sb.append("</tr>"); //Fill the data columns with the student's points for (Teststudent test : tests) { sb.append("<tr>"); sb.append("<td>"); sb.append(test.getTestId().getName()); sb.append(" - " + test.getTestId().getCourseId().getName()); sb.append("</td>"); sb.append("<td>"); sb.append(test.getScore()); sb.append("/20"); sb.append("</td>"); sb.append("</tr>"); } sb.append("</table>"); //Apply the CSS to our table CSSResolver cssResolver = new StyleAttrCSSResolver(); CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes())); cssResolver.addCss(cssFile); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); XMLWorker worker = new XMLWorker(css, true); XMLParser p = new XMLParser(worker); p.parse(new ByteArrayInputStream(sb.toString().getBytes())); document.close(); }