java 通过 iText 和 XMLWorker 使用波兰字母将 HTML 转换为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29102552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:41:39  来源:igfitidea点击:

Convert HTML to PDF by iText & XMLWorker with Polish Letters

javaitextxmlworker

提问by KurdTt-

I've got a string with an example in it - it works really great, but when I'm adding polish letters, they're gone. I tried something like this:

我有一个带有示例的字符串 - 它的效果非常好,但是当我添加波兰语字母时,它们就消失了。我试过这样的事情:

        byte[] byteArray = str.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        worker.parseXHtml(pdfWriter, document, byteArrayInputStream, Charset.forName("UTF-8"));

but it doesn't change anything. How to add polish letters?

但它不会改变任何东西。如何添加波兰语字母?

EDIT: It still doesn't work.

编辑:它仍然不起作用。

Code:

代码:

        document.open();

        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        String str = "<html><head></head><body style=\"font-size:12.0pt; font-family:Times New Roman\">"+
                "<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
                "<h1>Show your support</h1>" +
                "<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees</p>" +
                "<p>TEST POLSKICH ZNAKóW: ???Cóó????????</p>" +
                "<hr/>" +
                "<p>the huge amounts of time it takes for one person to design and write the actual content.</p>" +
                "<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?</p>" +
                "<p>Donate using PayPal?</p>" +
                "<p>Contributions via PayPal are accepted in any amount</p>" +
                "<p><br/><table border='1'><tr><td>Java HowTo</td></tr><tr>" +
                "<td style='background-color:red;'>Javascript HowTo</td></tr>" +
                "<tr><td>Powerbuilder HowTo</td></tr></table></p>" +
                "</body></html>";

        byte[] byteArray = str.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        worker.parseXHtml(pdfWriter, document, byteArrayInputStream, Charset.forName("UTF-8"));

        document.close();

Maybe someone will find a bug.

也许有人会发现一个错误。

回答by Bruno Lowagie

I have taken your sample HTML and I have used it to create the ParseHtml2example. The resulting PDF, html_2.pdf, looks like this:

我已经采用了您的示例 HTML,并使用它来创建ParseHtml2示例。生成的 PDF html_2.pdf如下所示:

enter image description here

在此处输入图片说明

At first sight, I don't see any issues with the Polish characters.

乍一看,我认为波兰语字符没有任何问题。

The code I used looks like this:

我使用的代码如下所示:

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
    String str = "<html><head></head><body style=\"font-size:12.0pt; font-family:Times New Roman\">"+
            "<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
            "<h1>Show your support</h1>" +
            "<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees</p>" +
            "<p>TEST POLSKICH ZNAKóW: \u0104\u0105\u0106\u0107\u00d3\u00f3\u0141\u0142\u0179\u017a\u017b\u017c\u017d\u017e\u0118\u0119</p>" +
            "<hr/>" +
            "<p>the huge amounts of time it takes for one person to design and write the actual content.</p>" +
            "<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?</p>" +
            "<p>Donate using PayPal\u017d</p>" +
            "<p>Contributions via PayPal are accepted in any amount</p>" +
            "<p><br/><table border='1'><tr><td>Java HowTo</td></tr><tr>" +
            "<td style='background-color:red;'>Javascript HowTo</td></tr>" +
            "<tr><td>Powerbuilder HowTo</td></tr></table></p>" +
            "</body></html>";

    XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
    InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
    worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"));
    // step 5
    document.close();
}

Note that you have defined Times New Romanas the font. It is essential that your OS has access to a font with that name, otherwise you'll still end up with Helvetica.

请注意,您已定义Times New Roman为字体。您的操作系统必须能够访问具有该名称的字体,否则您最终仍会使用 Helvetica。

Also be aware that using non-ASCII characters in source code is considered a crime against good taste. Source code is stored as a text file, but using which encoding? There is no guarantee that your file will be stored as UTF-8, there is no guarantee that a compiler will read it as UTF-8, there is no guarantee that a versioning system will accept UTF-8,... Hence I replaced all UTF-8 characters by their unicode value which allows me to keep the source file in ASCII.

另请注意,在源代码中使用非 ASCII 字符被视为违反品味的犯罪。源代码存储为文本文件,但使用哪种编码?无法保证您的文件将存储为 UTF-8,无法保证编译器会将其读取为 UTF-8,也无法保证版本控制系统会接受 UTF-8,...因此我替换了所有 UTF-8 字符按其 unicode 值,这使我可以将源文件保存为 ASCII。

回答by Nitish Saini

I have taken Bruno sample HTML and changed that function for C# users. I am using PdfFileName as a property to get and set the file name.

我采用了 Bruno 示例 HTML 并为 C# 用户更改了该功能。我使用 PdfFileName 作为属性来获取和设置文件名。

    public string PdfFileName { get; set; }
    public void CreatePdf()
    {
        // replace this code with you full pdf name which you want to create
        PdfFileName = EU.Master_Data_Utility.obj.Get_Current_DateTimeInteger(_connFlag) + ".pdf"; 

        String str = "<html><head></head><body style=\"font-size:12.0pt; font-family:Times New Roman\">" +
                "<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
                "<h1>Show your support</h1>" +
                "<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees</p>" +
                "<p>TEST POLSKICH ZNAKóW: \u0104\u0105\u0106\u0107\u00d3\u00f3\u0141\u0142\u0179\u017a\u017b\u017c\u017d\u017e\u0118\u0119</p>" +
                "<hr/>" +
                "<p>the huge amounts of time it takes for one person to design and write the actual content.</p>" +
                "<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?</p>" +
                "<p>Donate using PayPal\u017d</p>" +
                "<p>Contributions via PayPal are accepted in any amount</p>" +
                "<p><br/><table border='1'><tr><td>Java HowTo</td></tr><tr>" +
                "<td style='background-color:red;'>Javascript HowTo</td></tr>" +
                "<tr><td>Powerbuilder HowTo</td></tr></table></p>" +
                "</body></html>";

        StringReader sr = new StringReader(str.ToString());
        Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(PdfFileName), FileMode.Create));
        doc.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, sr);
        doc.Close();
        // Created a new function to open created file
        OpenPDFFile();

    }
    protected void OpenPDFFile()
    {
        //Open the PDF file
        Process.Start(Server.MapPath(PdfFileName));
    }