You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (JIRA)" <ji...@apache.org> on 2013/11/07 17:15:17 UTC
[jira] [Comment Edited] (PDFBOX-1689) Partial failure to render PDF
[ https://issues.apache.org/jira/browse/PDFBOX-1689?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13779789#comment-13779789 ]
Tilman Hausherr edited comment on PDFBOX-1689 at 11/7/13 4:13 PM:
------------------------------------------------------------------
I've had a long look at why the Elvia file (which uses TT fonts but doesn't have them embedded) isn't rendered correctly. The problem is that OS built-in TT fonts aren't used in PDTrueTypeFont.java. This is likely because the location of the OS TT files are OS dependent. PDTrueTypeFont.getExternalFontFile2() only takes the three fonts that are in the file PDFBox_External_Fonts.properties and in the location org/apache/pdfbox/resources/ttf. Any font that isn't found, including Symbol (!), is mapped to ArialMT.
So one solution that works is to tell getExternalFontFile2() where to find my fonts. Adding three lines to PDFBox_External_Fonts.properties
ArialNarrow=C:/Windows/Fonts/ARIALN.TTF
ArialNarrow,Bold=C:/Windows/Fonts/ARIALNB.TTF
Symbol=C:/Windows/Fonts/SYMBOL.TTF
brought a nicely rendered image. It also improved other files I use for testing, e.g. the cloud.pdf file.
Alternatively, I tried forcing rendering with awt by altering getExternalFontFile2() so that it returns null when it didn't find a TT font. This didn't work properly for the symbol font.
So a workable solution would be either to include more TT fonts (license????), or to find out the location the of OS TT files. This is tricky, it involves undocumented functions, it is OS and JDK dependent, and it also requires some mapping of the font names, similar to what is done in FontManager.java. It might fail in secure environments. The method is explained here:
http://stackoverflow.com/questions/2019249/get-font-file-as-a-file-object-or-get-its-path
http://stackoverflow.com/questions/13684342/jdk7-sun-font-fontmanager-replacement-how-to-get-filename-information-from-font
Alternatively, one might think about creating a tool that makes a setup, i.e. fills the file PDFBox_External_Fonts.properties
Or one might include TT fonts that have a "compatible" license.
What do you think? What should be done?
I could try to create such a tool. The following code gives the location of a font on JDK7, don't know if it works on JDK6:
{code}
static FontManager fontManager = FontManagerFactory.getInstance();
static public String getFontPath(String fontName, int fontStyle)
{
try
{
Font2D f2d = fontManager.findFont2D(fontName, fontStyle, FontManager.LOGICAL_FALLBACK).handle.font2D;
Field platName = PhysicalFont.class.getDeclaredField("platName");
platName.setAccessible(true);
String fontPath = (String) platName.get(f2d);
platName.setAccessible(false);
return fontPath;
}
catch (Exception ex)
{
//ex.printStackTrace();
return "";
}
}
{code}
I also suggest to add this to getExternalFontFile2():
{code}
if ((baseFont != null) && (externalFonts.containsKey(baseFont)))
{
fontResource = externalFonts.getProperty(baseFont);
}
else
{
LOG.warn ("TTF font not found: " + baseFont + ", default will be used"); //TH this line new
}
{code}
so that people know when something isn't perfect.
was (Author: tilman):
I've had a long look at why the Elvia file (which uses TT fonts but doesn't have them embedded) isn't rendered correctly. The problem is that OS built-in TT fonts aren't used in PDTrueTypeFont.java. This is likely because the location of the OS TT files are OS dependent. PDTrueTypeFont.getExternalFontFile2() only takes the three fonts that are in the file PDFBox_External_Fonts.properties and in the location org/apache/pdfbox/resources/ttf. Any font that isn't found, including Symbol (!), is mapped to ArialMT.
So one solution that works is to tell getExternalFontFile2() where to find my fonts. Adding three lines to PDFBox_External_Fonts.properties
ArialNarrow=C:/Windows/Fonts/ARIALN.TTF
ArialNarrow,Bold=C:/Windows/Fonts/ARIALNB.TTF
Symbol=C:/Windows/Fonts/SYMBOL.TTF
brought a nicely rendered image. It also improved other files I use for testing, e.g. the cloud.pdf file.
Alternatively, I tried forcing rendering with awt by altering getExternalFontFile2() so that it returns null when it didn't find a TT font. This didn't work properly for the symbol font.
So a workable solution would be either to include more TT fonts (license????), or to find out the location the of OS TT files. This is tricky, it involves undocumented functions, it is OS and JDK dependent, and it also requires some mapping of the font names, similar to what is done in FontManager.java. It might fail in secure environments. The method is explained here:
http://stackoverflow.com/questions/2019249/get-font-file-as-a-file-object-or-get-its-path
http://stackoverflow.com/questions/13684342/jdk7-sun-font-fontmanager-replacement-how-to-get-filename-information-from-font
Alternatively, one might think about creating a tool that makes a setup, i.e. fills the file PDFBox_External_Fonts.properties
Or one might include TT fonts that have a "compatible" license.
What do you think? What should be done?
I could try to create such a tool. The following code gives the location of a font on JDK7, don't know if it works on JDK6:
static FontManager fontManager = FontManagerFactory.getInstance();
static public String getFontPath(String fontName, int fontStyle)
{
try
{
Font2D f2d = fontManager.findFont2D(fontName, fontStyle, FontManager.LOGICAL_FALLBACK).handle.font2D;
Field platName = PhysicalFont.class.getDeclaredField("platName");
platName.setAccessible(true);
String fontPath = (String) platName.get(f2d);
platName.setAccessible(false);
return fontPath;
}
catch (Exception ex)
{
//ex.printStackTrace();
return "";
}
}
I also suggest to add this to getExternalFontFile2():
if ((baseFont != null) && (externalFonts.containsKey(baseFont)))
{
fontResource = externalFonts.getProperty(baseFont);
}
else
{
LOG.warn ("TTF font not found: " + baseFont + ", default will be used"); //TH this line new
}
so that people know when something isn't perfect.
> Partial failure to render PDF
> -----------------------------
>
> Key: PDFBOX-1689
> URL: https://issues.apache.org/jira/browse/PDFBOX-1689
> Project: PDFBox
> Issue Type: Bug
> Affects Versions: 2.0.0
> Reporter: Tilman Hausherr
> Assignee: Andreas Lehmkühler
> Attachments: ELVIA-Reiserucktritt-Vollschutz-01.png, ELVIA-Reiserucktritt-Vollschutz-01.png, ELVIA-Reiserucktritt-Vollschutz.pdf
>
>
> The attached file has several issues when rendering:
> - The long "-" symbol replaced (top right, after "Reiseart: gültig für alle Reisearten")
> - The € (euro) symbol replaced (top right, after "maximaler Reisepreis: ")
> - The square (on win7) or bullet (on XP) symbol is replaced with a sum symbol
> - The whole text is not rendered in the same font than in acrobat reader
--
This message was sent by Atlassian JIRA
(v6.1#6144)