itextpdf HTML到PDF包含西里尔字母

时间:2015-01-05 12:57:24

标签: java html pdf character-encoding itext

我已经提出了另一个关于这个问题的问题,但我无法使其发挥作用。我改变了我的代码,所以现在它是这样的:

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();
        }
    }
}

我的问题是pdf没有显示西里尔字符。我知道如何使用不同的字符集和字体制作一个简单的pdf,但我想将html文件或字符串(在我的情况下,它是一个html字符串)转换为pdf。提前谢谢。

2 个答案:

答案 0 :(得分:2)

根据@ bruno-lowagie的评论,只需对您发布的代码进行一些小的更改,以使其在Windows上运行。有关如何指定特定字体的更多信息,请参阅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();
        }
    }
}

答案 1 :(得分:0)

我尝试了很多东西,但每次我都遗漏了一些东西。谢谢@BrunoLowagie和@SubOptimal。这是我使用自定义字体运行的代码。它还包含一个简单的html作为字符串,但是(在注释中)显示了如何使用实际的html和css文件。

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);
    }
}

我注意到在css / html中使用font-family: actual font that supports wished encoding;并且电子邮件客户端始终使用内联css非常重要。