Using a primary and a fallbackfont in Flying Saucer PDF generator - java

I'm having trouble getting flying saucer to use a secondary font for the glyphs/charachters which are not present in my main font.
The Java code I'm using for this purpose is more or less:
String result = getPrintHtmlContent(urlString);
result = CharacterConverter.replaceInvalidCharacters(result);
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver();
renderer.getFontResolver().addFont(FONTS_DIR_PATH + "ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont(FONTS_DIR_PATH + "droidsans/DroidSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont(FONTS_DIR_PATH + "droidsans/DroidSansBold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocumentFromString(result, "http://" + frontendHost + ":" + frontendPort + frontendContextRoot);
renderer.layout();
renderer.createPDF(os);
And the css:
body {
font-family: "Droid Sans", "Arial Unicode MS";
}
I have also included the fonts in the css by using the #font-face rule.
I am able to get this to work using either of the fonts separately, so there seems to be no problem with flying saucer finding the fonts or the css not rendering correctly.
If I on the other hand do as above and try to use both fonts the output PDF is only using Droid Sans...
Is it even possible to use a "fallback font" in flying saucer, as it is on websites?

I asked the same question on Flying Saucer developer community and got a reply:
https://groups.google.com/forum/#!topic/flying-saucer-dev/5p00ISwnxiw
In short the answer is NO, it is not possible to use a secondary font.

Related

HTML to PDF using flying saucer: bold text is blurry

I am using flying saucer (xhtmlrenderer) 9.1.22 to create PDF files from HTML content,
but the bold text in PDF looks blurry.
this is my code snippet:
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("arial.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
renderer.setDocumentFromString(htmlAsString);
renderer.layout():
renderer.createPDF(output);

Change BaseFont size in ItextPdf

I am using arial font using BaseFont in ItextPdf in my Java app. I am updating the acro fields of an editable pdf and regenerating. It's working all fine.
I am creating it as:
ArrayList<BaseFont> fonts = new ArrayList<BaseFont>();
BaseFont baseFont = BaseFont.createFont("/path/to/arial.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
fonts.add(baseFont);
and within my acroform I am attaching that font like this:
acroForm.setSubstitutionFonts(fonts);
How can I change the size of this BaseFont?

PDFTable Itext arabic

I have coded java code and I wanted Arabic words to be displayed at PdfPTable which was asses to itext document to create PDF document
as attached picture "???" is Arabic code '
PdfPTable header = new PdfPTable(6);
PdfPTable tbame = new PdfPTable(1);
tbame.addCell(" >>>>>> " + install.getCustId().getFullName() + " <<<<<<");
tbame.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
tbame.setLockedWidth(false);
tbame.setExtendLastRow(false);
tbame.setWidthPercentage(100);
header.addCell("End");
header.addCell("Start");
Please read the documentation and you'll find out that the addCell(String content) method can not be used to add Arabic text for two reasons:
When you use this method, the default font Helvetica is used. You need to use a font that knows how to draw Arabic shapes. This is explained in the answer to this question: Itext Arabic Font coming as question marks
Arabic is written from right to left, which means that you need to change the run direction of the content of the cell as is explained in my answer to this question: RTL not working in pdf generation with itext 5.5 for Arabic text
A code snippet:
BaseFont bf = BaseFont.createFont("c:/WINDOWS/Fonts/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Phrase phrase = new Phrase(
"\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628", font);
PdfPCell cell = new PdfPCell(phrase);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
If you don't have access to the font arialuni.ttf, you'll have to find another font that contains Arabic glyphs.

Convert HTML to PDF with Special Characters using Java

I am using flying saucer with iText 2.1.7 for converting html to pdf. It works fine, but the problem occurs when there are some chinese, korean, etc characters in the html.
I get unexpected characters in my PDF instead of the normal chinese characters
I found this issue opened, so I assume there is currently no way of making flying saucer into rendering the PDF correctly?
PS: I also found this issue, but I can't understand the solution they have provided.
This is the code that I am using
String doc = file.toURI().toURL().toString();
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont (
"C:\\ARIALUNI.TTF",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED
);
renderer.setDocument(doc);
String outputFile = "report.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
Where file is the html which I am trying to convert.
Is there some other way or library to do the same?
This is the css that i am using
#font-face {
font-family: "Arial";
src: url("media/arialuni.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
The HTML file that I need to convert
These are the re-compiled flying saucer jar compatible with itext 2.1..x
Your font is probably not embedded in the PDF file. ( How do I know if the fonts in a PDF file are embedded or not? )
Every font has a name, ARIALUNI.TTF defines Arial Unicode MS, you should use that.
So change this:
#font-face {
font-family: Arial1;
src: url("arialuni.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
* {
font-family: Arial1;
}
To this:
#font-face {
font-family: Arial Unicode MS;
src: url("arialuni.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
* {
font-family: Arial Unicode MS;
}
This way the font will be embedded.
And you don't need to call renderer.getFontResolver().addFont, the css is enough.
Try this:
font.addFont(Html2Pdfs.class.getResource("SIMSUN.TTC").toString().substring(6),BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED)

How to get less-than-equal-to to show in iTextRenderer conversion

I've tried everything I know but still cannot get the ≥ and ≤ characters to show up in my PDF generated by Flying Saucer's iTextRenderer from xHTML. They display fine in my browser as HTML of course. I've tried different xml encoding types, doctypes, and using the decimal and hex values instead of the shortcut names. Still, no errors display, but I get a blank space where the characters should be.
Has anyone successfully converted an xhtml to pdf using iTextRenderer with the ≤ and ≥ special symbols, and if so, how?
SOLUTION: Okay, I got this one solved. Turns out I did need to embed the Verdana font into the PDF using the follow lines of Java code:
renderer.getFontResolver().addFont("/Library/Fonts/Verdana.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
SOLUTION: Okay, I got this one solved. Turns out I did need to embed the Verdana font into the PDF using the follow lines of Java code:
renderer.getFontResolver().addFont("/Library/Fonts/Verdana.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/Library/Fonts/Verdana Bold Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

Categories