prasath-ideas2it / flying-saucer

Automatically exported from code.google.com/p/flying-saucer
0 stars 0 forks source link

Convert Multilingual content to pdf #91

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
*** This issue was imported from http://java.net/jira/browse/XHTMLRENDERER-199

It was reported by nskclr on 14.09.2007 18:52:32 +0200 and last updated in the 
previous bug tracker on 20.07.2008 19:29:06 +0200

Found in
Operating System: All
Platform: All

The priority for this issue at migration was Major.

Original description: 
I use the below code to convert multilingual xhtml to pdf. But the pdf 
generated has dots. What should I do to get the desired pdf output?

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os); 

I even tried with other language with the code below

ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
renderer.getFontResolver().addFont("C:\\WINDOWS\\FONTS\\DVDIVEB0.TTF", true);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os); 

But still got the same output.

Original issue reported on code.google.com by pdoubl...@gmail.com on 16 Feb 2011 at 9:55

GoogleCodeExporter commented 9 years ago
nskclr wrote on 20.09.2007 20:11:36 +0200:
Hi 

I installed ARIALUNI.TTF & tried with the code below.
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("C:\\WINNT\\Fonts\\ARIALUNI.TTF",
"UTF-8", BaseFont.NOT_EMBEDDED);

Still the pdf is incorrect. Do I need to change any pdf settings?

Thanks

Original comment by pdoubl...@gmail.com on 16 Feb 2011 at 9:55

GoogleCodeExporter commented 9 years ago
nskclr wrote on 21.09.2007 18:44:58 +0200:
Hi Pete

Thanks for your response.

From the flying saucer source, I got css for 4 locales. I need help to get the 
css for other languages like chinese, korean, japanese etc.

Thanks

Original comment by pdoubl...@gmail.com on 16 Feb 2011 at 9:55

GoogleCodeExporter commented 9 years ago
pdoubleya wrote on 20.07.2008 19:29:06 +0200:
Deferring to R9.

Original comment by pdoubl...@gmail.com on 16 Feb 2011 at 9:55

GoogleCodeExporter commented 9 years ago
I've been able to render UTF-8 using code similar to the above but using 
BaseFont.IDENTITY_H as the user guide shows.

Since fonts must be provided for UTF-8, I'd like to be able to provide a 
classpath file (such as byte content) for the font file instead of a full file 
path.
IText BaseFont supports creating fonts using the byte content as the param 
ttfAfm, but defaults to using the file path when missing.

I attempted to create a custom FontResolver from ITextFontResolver, but too 
many methods and inner classes are private 
(for example getFontFamily() is public but it returns FontFamily which is 
private)

For CSS, the url can be provided as relative but I believe it's still expected 
to be relative to the current directory (based on NaiveUserAgent.resolveURI)

Original comment by matt.blanchette@gmail.com on 2 Oct 2012 at 10:07

GoogleCodeExporter commented 9 years ago
I was able to use the CSS approach, creating a custom user agent extending 
ITextUserAgent and overriding resolveAndOpenStream() to also check the 
classpath with getClassLoader().getResourceAsStream

For the CSS I used this for each of the files (changing the weight/style for 
each to match the name):
@font-face {
    font-family: "___";
    src: url("fonts/___-Regular.ttf");
    font-weight: normal;
    font-style: normal;
    -fs-pdf-font-embed: embed;
    -fs-pdf-font-encoding: Identity-H; 
}

Original comment by matt.blanchette@gmail.com on 3 Oct 2012 at 3:19

GoogleCodeExporter commented 9 years ago
@matt.blanchette@gmail.com, What did you use? I have tried using font-face in 
css and also addFont function but i am not able to print the Chinese 
characters, Can you please help me

Original comment by ankurmit...@gmail.com on 17 Nov 2012 at 10:47

GoogleCodeExporter commented 9 years ago
With the font-face CSS I mentioned before I also used the code below.
This loads the font files from the classpath (based on src url in CSS).

/**
 * Allows overriding read/opening streams from URI to allow resolving fonts from classpath.
 */
public class FontResourceLoader extends ITextUserAgent {
    public FontResourceLoader(ITextOutputDevice outputDevice) {
        super(outputDevice);
    }
    @Override
    protected InputStream resolveAndOpenStream(String uri) {
        InputStream is = super.resolveAndOpenStream(uri);
        // Allow resolving fonts from classpath
        if( is == null && uri != null && uri.contains("fonts/") ) {
            is = FontResourceLoader.class.getClassLoader().getResourceAsStream(uri);
        }
        return is;
    }
}

ITextRenderer pdfRenderer = new ITextRenderer();
// Add custom user agent for resolving url resource lookups for fonts from 
classpath
FontResourceLoader fontHandler = new 
FontResourceLoader(pdfRenderer.getOutputDevice());
fontHandler.setSharedContext(pdfRenderer.getSharedContext());
pdfRenderer.getSharedContext().setUserAgentCallback(fontHandler);

Original comment by matt.blanchette@gmail.com on 20 Jan 2014 at 3:19

GoogleCodeExporter commented 9 years ago
Regarding loading fonts from the classpath:

If all you want to do is loading a specific font file from the classpath, you 
do not need any special handlers, as iText 2.1.7 actually tries to load fonts 
as a resource from the classpath as a last resort.

renderer.getFontResolver().addFont("/the/package/of/the/font.ttf", 
BaseFont.IDENTITY_H, true);

will load the font from the classpath just fine.

In fact, if you do not want to use the CSS descriptor route detailed above, 
this is the only way that does not require copying and hacking half the font 
logic classes in iText to make it work.

Original comment by stot...@gmail.com on 9 Oct 2014 at 2:20