问题
I am using iText 5.4.1 API to convert HTML to PDF, for English HTML the GIF, JPG images are coming good and same size in PDF as with HTML, but when I use Brazil country HTML the GIF, JPG images are very small. Please find the code snippet below.
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFileNameWithPath));
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = XMLWorkerHelper.getInstance()
.getDefaultCssResolver(true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext, new
PdfWriterPipeline(document,
writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
File input = new File(completeHtmlFilePath);
p.parse(new InputStreamReader(new FileInputStream
(input), "UTF-8"));
document.close();
回答1:
Please take a look at the following screen shot:

In the lower-left corner, you see some HTML in Portuguese:
<html>
<body>
<div><b>Brasil</b>, oficialmente República Federativa do Brasil, é o maior país da América do Sul
e da região da América Latina, sendo o quinto maior do mundo em área territorial (equivalente
a 47% do território sul-americano) e população (com mais de 202 milhões de habitantes).
É o único país na América onde se fala majoritariamente a língua portuguesa e o maior país lusófono
do planeta, além de ser uma das nações mais multiculturais e etnicamente diversas,
em decorrência da forte imigração oriunda de variados cantos do mundo.</div>
<img src="resources/images/brasil.png" />
</body>
</html>
This HTML was converted to PDF using iText 5.5.6 and this code:
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML));
// step 5
document.close();
}
As you can see, the Portuguese text renders correctly, as well as the image. I have strong suspicions that the problem is caused:
- either by some peculiarities in your Portuguese HTML,
- or by the fact that you're using an iText version that is more than two years old.
There could be encoding or font issues too, but you are talking about images not being rendered correctly. I'd expect your question to be about special characters if encoding or font issues were at play.
来源:https://stackoverflow.com/questions/30206803/itext-html-to-pdf-for-non-english-content-images-are-very-small-in-pdf