how to convert HTML page to PDF using ITEXT or PDFCROWD API - itext

PDF created will be based on dynamic HTML page.
Using ITEXT 5 or 7 with XMLWORKERHELPERCLASS would be lengthy process.
If i am using pdfcrowd API it seems to be ok but not able to generate on localhost or any other private ip. I am ready to pay for their services if they achieve above issue.

First you need to get iText 7 (the core library) and the pdfHtml add-on (the part that will parse the HTML+CSS and convert it to iText objects). Go to github to find out how to download these.
Suppose that you have this HTML:
With this corresponding CSS:
Then you can use this code snippet:
ConverterProperties converterProperties =
new ConverterProperties().setBaseUri(resoureLocation);
HtmlConverter.convertToPdf(
new FileInputStream(HTMLSource),
new FileOutputStream(pdfDestination), converterProperties);
Where resourceLocation points at your base URI, HTMLSource is the path to your HTML file, and pdfDestination is the path where you want the resulting PDF to be written.
When you execute this code, you will get the following PDF:
Note that buying a commercial license may be necessary if you intend to use iText in the context of a proprietary software project.

Related

Generate multiple sheets in one pdf file

I am trying to generate a pdf from a Tableau workbook which has two sheets using the url method:
E.g: https://TableauServer/views/workbook/sheet1?:format=pdf&parameter=value
I am doing this in a program which will issue the url request to the url. The url works fine for one sheet. But the problem is how to generate one pdf file with both sheets in it?
If you first put your two sheets into a single dashboard and then use the URL for the published dashboard (still using the format=pdf parameter), this should work just fine.
We know it's possible because within the Tableau pages itself if you download a PDF it gives you several formatting options, including the option to put all the worksheets in a workbook into a single PDF.
I couldn't find any documentation on it though. What I ended up doing was looking at the network console in the browser (usually F12) when I downloaded the PDF from the browser by clicking the Download button. That showed me the URL end point and the JSON body the server expected in the request payload.
The endpoint URL wasn't too cryptic and ended with "commands/tabsrv/pdf-export-server". The challenge was to take the JSON in the request payload and find the right settings to get it into a single PDF.
This method is a more technical approach and requires very little coding skills; any language that has functions for http calls will work (I use python for it).
If you don't mind doing it outside a browser, tabcmd has lots of functionality to control PDF generation at the command line.

iTextSharp 5: Create image from text

I am using iTextSharp (C#) to generate a few PDF-reports.
One of them creates a bill. The bill must contain one line in OCR-B.
I cannot embed the font file.
Since im doing the new reports after the old ones, I went to check how it was done in the old bill-report. They inserted a picture.
Seems like a good workaround.
I have been googling on how to render text as image using iTextSharp, without success.
I am open for suggestions.
Apparently, there is no iTextSharp way of doing this.
This approach worked for me How to generate an image from text on fly at runtime
That way I didn't have to include any 3rd party libraries.

Tableau: can a visualization be created via javascript or other language?

I am trying to create a new visualization (sheet) in a tableau online workbook via javascript API or by another language. Not by using Tableau Desktop or "manual" interaction into Tableau Online.
I know that the JS API allows me to control (filter, display, etc.) existing visualizations, and the SDK can extract data and publish, but my need is to "create" a new visualization into an existing workbook.
Is there a way to do this?
The only methods of creating Tableau content that I'm aware of is using Tableau Desktop or Web authoring of something that is already published.
I explain how to do what you're asking on the blog post linked below. You can use Python with Jinja2.
The basics ...
Create a template of your XML.
Put in the necessary Jinja2 templating language code into your template as placeholders for the data and XML that needs to be rendered by Jinja2. You can render data conditionally as well.
Create a CSV file that specifies what the Python program needs to know to create your workbooks.
Run your Python application to generate a TWB file based on your template and input file. You can also easily create TWBX by zipping the TWB and data together.
The link gives code examples and an example CSV file for specifying your input.
https://www.linkedin.com/pulse/create-tableau-visualizations-programmatically-allan-thompson

itext JavaScript version

first time here posting, though I have benefited many times from other postings on stackoverflow. I am the principal developer of Jmol, which I have recently ported to JavaScript/HTML5. The Jmol applet interfaces with another applet, JSpecView, which in its current form as a Java applet utilizes a slimmed down version of itext.1.4.5.jar to create PDF files of spectra.
I am interested in converting iTtext Java code to JavaScript so that a JavaScript version of the JSpecView applet can create PDF files directly.
Q: Has this already been accomplished?
Q: JSpecView is an Open-Source project licensed under LGPL. All our source code is available. Is this a problem?
Bob Hanson
Principal Developer, Jmol/JSmol
A: I don't think such a port has been done before. But why would you not use an existing JavaScript pdf library? Or what is wrong with doing the PDF generation on the server side? Why would you do more work than is necessary? I've done a quick google on JS PDF libraries and found:
http://jspdf.com/
https://github.com/mozilla/pdf.js
And the following stackoverflow question:
Generating PDF files with Javascript
A: I think it shouldn't be a problem porting it to JavaScript as you'd be porting to the same license (of course, if you're using a version of iText before the license switch to AGPL), but IANAL.

How to export a JasperReport to HTML, without saving the images on a disk?

I'm trying to introduce a HTML export to our JasperReports based report engine. The HTML document is generated fine, but the problems start when it contains images. The images are not part of the HTML in contrast with PDF.
The solution I found so far is the following:
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, "./images/");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/images/");
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();
This way all the images are automatically generated by JasperReports and saved in this "images" folder. They are also correctly referenced by the HTML file.
The problem is that the images are saved too late in the folder and the user needs to refresh the already received HTML page in order to see them. This is really not nice and also I don't see why the images should be saved, since they are automatically generated by JasperReports anyway.
How can I tell JasperReports to store the images in memory somehow and how the user will access them in this case?
Check the Jasper examples (part of the source distribution), it has a 'webapp' example that shows how you can use an image servlet for that end.