Itextpdf HTML to PDF containing Cyrillic letters
I asked another question about this issue, but I couldn't get it to work. I changed my code so it now looks something like this:
import java.io.FileOutputStream;
import java.io.StringReader;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class HTM {
public static void main(String ... args ) {
try {
Document document = new Document(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.getInstance
(document, new FileOutputStream("C:\\testpdf.pdf"));
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
String htmlString = "<html><head>"
+ "<meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=UTF-8\" />"
+ "</head><body>"
+ "<h1>Zdravo Ρ!</h1>"
+ "</body></html>";
worker.parseXHtml(pdfWriter, document, new StringReader(htmlString));
document.close();
System.out.println("Done.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
My problem is that cyrillic characters are not displayed in the pdf. I know how to make a simple pdf with different encodings and fonts, but I want to convert an html file or string (in my case it is an html string) to pdf. Thanks in advance.
source to share
I tried a lot of things, but every time I was missing something. Thanks to @BrunoLowagie and @SubOptimal. Here's my code that I am running for custom fonts. It also contains plain html as a string, but the comments show (how this is done) how this can be done with the actual html and css files.
public class HtmlToPdf {
public static final String DEST = "/home/christian/Desktop/testDoc.pdf";
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setInitialLeading(12.5f);
// step 3
document.open();
// step 4
// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
// CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
// cssResolver.addCss(cssFile);
// HTML
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("fonts/Arimo-Regular.ttf");
fontProvider.register("fonts/Arimo-Bold.ttf");
fontProvider.register("fonts/Arimo-Italic.ttf");
fontProvider.addFontSubstitute("lowagie", "Arimo");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
// p.parse(new FileInputStream(HTML));
String htmlContent = " HERE GOES HTML CODE ";
p.parse(new StringReader(htmlContent));
// step 5
document.close();
}
public static void main(String[] args) throws IOException, DocumentException {
new D06_ParseHtmlFonts().createPdf(DEST);
}
}
I noticed that it is important to have font-family: actual font that supports wished encoding;
in css / html and for email clients always use inline css.
source to share
Based on a comment from @ bruno-lowagie, only a small change to your posted code is required to get it working on Windows. For more information on how to specify a specific font, see the examples suggested by Bruno.
public class HTM {
public static void main(String ... args ) {
try {
Document document = new Document(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("testpdf.pdf"));
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
String htmlString = "<html><head>"
+ "<meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=UTF-8\" />"
+ "</head><body>"
+ "<p style=\"font-family:courier new\">" // the font to use
+ "<h1>Zdravo Ρ!</h1>"
+ "</p>"
+ "<h1>Zdravo Ρ!</h1>"
+ "</body></html>";
worker.parseXHtml(pdfWriter, document, new StringReader(htmlString));
document.close();
System.out.println("Done.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
source to share