Unable to set Page margin in flying-saucer - java

I am generating pdf from html. I am using dynamic html generator(Apache-velocity) and using jsoup for html parsing. I generate pdf successfully, issue is html content not aligned properly in pdf after generation. I already visited this question unluckily its unanswered.

Margin can be set in html in head tag using style tag. Sample for adding 1px margin is shown. In my case for flying saucer library it worked well with this.
<head><style>#page {size: Wpx 1px;margin:0px;}<style/><head/>

Related

JavaFx html formatted text in pdf using iText with formatation

Is it possible to set a formatted HTML-Text (Color, Alignment, ...) from a HTMLEditor to an "editable" PDF using iText.
I didn't find anything on the internet.
Thanks.
The easiest way of doing this is (as Amedee suggested) using pdfHTML.
It's an iText7 add-on that converts HTML5 (+CSS3) into pdf syntax.
The code is pretty straightforward:
HtmlConverter.convertToPdf(
"<b>This text should be written in bold.</b>", // html to be converted
new PdfWriter(
new File("C://users/user2002/output.pdf") // destination file
)
);
To learn more, go to https://itextpdf.com/itext7/pdfHTML
I found a Solution in this post using The Flying Saucer: this

Adding Header or Footer on every page using ITextRenderer from HTML

I'm creating an HTML report usgin freemarker, and i produce a PDF from that HTML using ITextRenderer.
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(baosPDF);
I have a table in that html, with a header that successfully shows on every page using css classes:
thead { display:table-header-group }
Is it possible to do the same trick for an arbitrary section of my document? (let say, a div) I'ld like to keep my html vanilla, and identify the "header" and "footer" i want to see on every page using css.
Is it possible, only with css?
Perhaps you should have a look at
http://developers.itextpdf.com/content/itext-7-examples/converting-html-pdf
It gives a few examples of converting html to pdf. Including loading an external stylesheet.

HTML -> PDF rendering issues

I'm generating pdfs from HTML pages with an application. Sometimes, the pdf is formatted correctly (with styles); other times, it lacks style elements.
In the log file I can see the "Error in rendering".
We are using HTML tags and using string buffer we are converting html tag to pdf file. Not sure why we are getting this missing format issues while generating the pdf file.
So sometimes the CSS file (style) does convert with the HTML file, and sometimes the CSS doesn't convert with the HTML file.
I'm guessing that you use an external CSS file. If I were you, I would try to type your CSS code inside your HTML file, under the header element, like this:
<style>
body {background-color:#fff}
h1 {color:#eee}
</style>

using JSF PrimeFaces' text editor, how to add text in PDF using iText

We are using JSF PrimeFaces' text editor. When we receive String from text editor in backing bean, it also includes HTML tags. Following image might help in understanding this problem.
Following is what we wrote:
Following is what we received:
Next thing we want to do is, to write what was written in text editor, as it is, into PDF using iText. But we do not know how to convert this string (with HTML tags) into only data.
Following was the code:
You can go for XMLWorker in iText. Below code will give you the content in Orange color
document.open();
String finall= "<style>h1{color:orange;} </style><body><h1>This is a Demo</h1></body>";
InputStream is = new ByteArrayInputStream(finall.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is);
document.close();
Whatever HTML content we are giving it will create it as PDF. The only thing to take care is it will work for XHTML means all the opening tags should have a end tag.
For example in HTML for break we will use <br> but here it should be <br/>Hope this will help you.
Use JSoup to achieve this. Jsoup
Then in your code
Jsoup.parse(textRecievedFromEditor).text();
This will return text without HTML Tags.
e.g.
For example, given HTML {#code <p>Hello <b>there</b> now!</p>},
{#code p.text()} returns {#code "Hello there now!"}

HTML content to pdf in JAVA [duplicate]

Does anyone know if it is possible to convert a HTML page (url) to a PDF using iText?
If the answer is 'no' than that is OK as well since I will stop wasting my time trying to work it out and just spend some money on one of a number of components which I know can :)
I think this is exactly what you were looking for
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
http://code.google.com/p/flying-saucer
Flying Saucer's primary purpose is to render spec-compliant XHTML and CSS 2.1 to the screen as a Swing component. Though it was originally intended for embedding markup into desktop applications (things like the iTunes Music Store), Flying Saucer has been extended work with iText as well. This makes it very easy to render XHTML to PDFs, as well as to images and to the screen. Flying Saucer requires Java 1.4 or higher.
I have ended up using ABCPdf from webSupergoo.
It works really well and for about $350 it has saved me hours and hours based on your comments above.
The easiest way of doing this is using pdfHTML.
It's an iText7 add-on that converts HTML5 (+CSS3) into pdf syntax.
The code is pretty straightforward:
HtmlConverter.convertToPdf(
"<b>This text should be written in bold.</b>", // html to be converted
new PdfWriter(
new File("C://users/mark/documents/output.pdf") // destination file
)
);
To learn more, go to http://itextpdf.com/itext7/pdfHTML
The answer to your question is actually two-fold. First of all you need to specify what you intend to do with the rendered HTML: save it to a new PDF file, or use it within another rendering context (i.e. add it to some other document you are generating).
The former is relatively easily accomplished using the Flying Saucer framework, which can be found here: https://github.com/flyingsaucerproject/flyingsaucer
The latter is actually a much more comprehensive problem that needs to be categorized further.
Using iText you won't be able to (trivially, at least) combine iText elements (i.e. Paragraph, Phrase, Chunk and so on) with the generated HTML. You can hack your way out of this by using the ContentByte's addTemplate method and generating the HTML to this template.
If you on the other hand want to stamp the generated HTML with something like watermarks, dates or the like, you can do this using iText.
So bottom line: You can't trivially integrate the rendered HTML in other pdf generating contexts, but you can render HTML directly to a blank PDF document.
Use itext libray:
Here is the sample code. It is working perfectly fine:
String htmlFilePath = filePath + ".html";
String pdfFilePath = filePath + ".pdf";
// create an html file on given file path
Writer unicodeFileWriter = new OutputStreamWriter(new FileOutputStream(htmlFilePath), "UTF-8");
unicodeFileWriter.write(document.toString());
unicodeFileWriter.close();
ConverterProperties properties = new ConverterProperties();
properties.setCharset("UTF-8");
if (url.contains(".kr") || url.contains(".tw") || url.contains(".cn") || url.contains(".jp")) {
properties.setFontProvider(new DefaultFontProvider(false, false, true));
}
// convert the html file to pdf file.
HtmlConverter.convertToPdf(new File(htmlFilePath), new File(pdfFilePath), properties);
Maven dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.1.3</version>
</dependency>
Use iText's HTMLWorker
Example
When I needed HTML to PDF conversion earlier this year, I tried the trial of Winnovative HTML to PDF converter (I think ExpertPDF is the same product, too). It worked great so we bought a license at that company. I don't go into it too in depth after that.

Categories

Resources