We are coverting HTML to PDF using Itext and xmlworker 5.5.5. Following is the code. Issue is that we are not able to see radio or checkboxes rendered on pdf. What additional things that are needed for checkbox and radio box ?
cssStr - Containing all the css classes. Its string.
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destFile));
writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
// step 3
document.open();
// step 4 - Styles
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(cssStr.getBytes()));
cssResolver.addCss(cssFile);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register(OLTContext.getWebappDir()+"/bs/fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H);
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
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);
ByteArrayInputStream stream = new ByteArrayInputStream(printable.getBytes("UTF-8"));
p.parse(stream, Charset.forName("UTF-8"));
// step 5
document.close();
Also I tried itext 7 with trial license of pdfHtml. Radio & Checkboxes or basically any input elements of HTML are not rendered at all. Following code
LicenseKey.loadLicenseFile("/Users/ashish/server-ws/workspace/Test/lib/itextkey-0.xml"); HtmlConverter.convertToPdf(new File(HTML), new File(DEST));
Unless you have a very good reason to be using XMLWorker, I suggest you try pdfHtml. It's an addon we released for iText7 that offers support for HTML5 and CSS3.
Trial license can be obtained for free at the iText website. And the next release of pdfHtml should be AGPL licensed and open source (we are currently doing some final code cleanup).
Related
[]
1[]2]3
Here is my code to add svg image in the pdf using itext 7.1.3 and also using SVGConventer.The image not properly rendered, i am not able understand where and what is the problem? Can anyone help me. Thanks in advance.
PdfWriter writer = new PdfWriter(new FileOutputStream("/home/users/Documents/pdf/new.pdf"));
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
URL svgUrl = new File(svg).toURI().toURL();
doc.add(new Paragraph("new pikachu"));
Image image = SvgConverter.convertToImage(svgUrl.openStream(), pdfDoc);
doc.add(image);
doc.close();
I want to convert one html page to pdf using itext 2.1.7. I have used HTMLWorker to convert the html file, but it not taking the inline css which I have used in the html. Below is my code snippet . Can anyone help to fix this issue..
PdfWriter pdfWriter = PdfWriter.getInstance(document, new
FileOutputStream("D:/testpdf.pdf"));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlContent));
document.close();
Thanks in Advance !
Use itext7-7.0.2 because iText 2.1.7 didn't support inline CSS.
String htmlContent = "<html><body style='color:red'> PDF project </body></html>";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(new File("C:\\testpdf.pdf")));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlContent));
document.close();
How to set PDF page size A4 when we use ITextRenderer to generate PDF from thymeleaf HTML template ?
I have generated PDF but page size is not proper, how to set page size A4 ITextRenderer library in JAVA
ClassLoaderTemplateResolver templateResolver = new
ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
Context context = new Context();
context.setVariable("name", "Thomas");
String html = templateEngine.process("templates/Quote", context);
OutputStream outputStream = new FileOutputStream("message.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(outputStream,true);
outputStream.close();
Please be aware that you are using FlyingSaucer, not iText.
FlyingSaucer is a product that internally uses (a very old version of) iText.
You are immediately cutting yourself off from 10+ years of bugfixes and developments.
If you are comfortable going for just iText, the best way of solving this issue is with pdfHTML.
It's an add-on we wrote to the iText7 core library that is specifically designed to convert HTML into PDF.
Simple example:
File src = new File("source_html_file.html");
File dest = new File("target_pdf_file.pdf");
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdf, PageSize.A4);
InputStream stream = new FileInputStream(src);
ConverterProperties converterProperties = new ConverterProperties();
FontProvider dfp = new DefaultFontProvider(true, true, true);
converterProperties.setFontProvider(dfp);
HtmlConverter.convertToPdf(stream, pdf, converterProperties);
Check out the tutorials online for more information
https://developers.itextpdf.com/content/itext-7-examples/itext-7-converting-html-pdf
To fix this issue using jsoup, xhtmlrenderer from flying-saucer-pdf-openpdf just set this in your html-document:
#page {
size: A4;
}
I'm executing this code from Eclipse and on Tomcat into a webapp
FileInputStream is = new FileInputStream("C:/Users/admin/Desktop/dummy.txt");
try {
FontFactory.register("C:/Workspace/Osmosit/ReportManager/testSvn/ReportManagerCommon/src/main/java/com/osmosit/reportmanager/common/itext/fonts/ARIALUNI.TTF");
} catch (Exception e) {
e.printStackTrace();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
Document document = new Document(PageSize.A4);
PdfWriter writer;
writer = PdfWriter.getInstance(document, byteArrayOutputStream);
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
byteArrayOutputStream.close();
FileOutputStream fos = new FileOutputStream("C:/Users/admin/Desktop/prova-web.pdf");
fos.write(byteArrayOutputStream.toByteArray());
fos.close();
the dummy.txt is a simple html with aranic and latin characters
<div style="font-family: Arial Unicode MS;" ><p>كما. أي مدن العدّ وقام test latin</p><br /></div>
When I run under eclipse I obtain a correct pd, when it runs on Tomcat I get this:
كما. أي مدن العدّ وقام test latin
PS: I'm using itextpdf ver 5.5.8
You have an encoding problem. Either you saved dummy.txt using the wrong encoding (e.g. as Latin-1 instead of as UTF-8), or you are reading dummy.txt using the wrong encoding.
See html to pdf convert, cyrillic characters not displayed properly and adapt the line in which you use parseHtml():
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
is, null, Charset.forName("UTF-8"), fontImp);
Take a look at the ParseHtml11 example to find out what fontImp is about.
You are also making another mistake: Arabic is read from right to left, and in your code, you aren't defining the run direction. See Arabic characters from html content to pdf using iText
In your case, I would put the Arabic text in a table and I would follow the ParseHtml7 example from the official documentation:
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
// Styles
CSSResolver cssResolver = new StyleAttrCSSResolver();
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("resources/fonts/NotoNaskhArabic-Regular.ttf");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
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 FileInputStream(HTML), Charset.forName("UTF-8"));
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
for (Element e : elements) {
cell.addElement(e);
}
table.addCell(cell);
document.add(table);
// step 5
document.close();
}
I've a problem with iText.
I've followed this link: How to export html page to pdf format?
My snippet:
String str = "<html><head><body><div style=\"width:100%;height:100%;\"><h3 style=\"margin-left:5px;margin-top:40px\">First</h3><div style=\"margin-left:15px;margin-top:15px\"><title></title><p>sdasdasd shshshshdffgdfgd</p></div><h3 style=\"margin-left:5px;margin-top:40px\">The dream</h3><div style=\"margin-left:15px;margin-top:15px\"></div></div></body></head></html>";
String fileNameWithPath = "/Users/cecco/Desktop/pdf2.pdf";
com.itextpdf.text.Document document =
new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4);
FileOutputStream fos = new FileOutputStream(fileNameWithPath);
com.itextpdf.text.pdf.PdfWriter pdfWriter =
com.itextpdf.text.pdf.PdfWriter.getInstance(document, fos);
document.open();
document.addAuthor("Myself");
document.addSubject("My Subject");
document.addCreationDate();
document.addTitle("My Title");
com.itextpdf.text.html.simpleparser.HTMLWorker htmlWorker =
new com.itextpdf.text.html.simpleparser.HTMLWorker(document);
htmlWorker.parse(new StringReader(str.toString()));
document.close();
fos.close();
and work fine.
But tag style into h3 and div aren't considered.
But if I copy my html into http://htmledit.squarefree.com/ all is correct.
How can I solve this problem?
iText isn't the best Html Parser, but you can use Flying-Saucer for this. Flying-Saucer is build on top of iText but has a capable Xml / (X)Html parser. Short: Flying Saucer is perfect if you want html -> Pdf.
Here's how to generate the pdf from your string:
/*
* Note: i filled something in the title-tag and fixed the head tag (the whole body-tag was in the head)
*/
String str = "<html><head></head><body><div style=\"width:100%;height:100%;\"><h3 style=\"margin-left:5px;margin-top:40px\">First</h3><div style=\"margin-left:15px;margin-top:15px\"><title>t</title><p>sdasdasd shshshshdffgdfgd</p></div><h3 style=\"margin-left:5px;margin-top:40px\">The dream</h3><div style=\"margin-left:15px;margin-top:15px\"></div></div></body></html>";
OutputStream os = new FileOutputStream(new File("example.pdf"));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(str);
renderer.layout();
renderer.createPDF(os);
os.close();
But: FS supports only valid Html / Xhtml / xml, so make shure it is.