在iText 7的html2pdf中显示货币符号的简单方法

问题描述 投票:0回答:1

我根据iText 5.0的帖子将代码从iText 7更新为html2pdf 2.0this。在较早的版本中,卢比符号可以正确渲染,但是由于css问题,我更改了代码。现在,除卢比符号外,完整的页面都可以正确转换为pdf。

  1. 试图像html一样在* { font-family: Arial; }样式标签中添加字体。
  2. 更改了卢比符号的值,从&#x20b9₹,也直接添加了₹,但无用。

我的HTML:

<html>
<head>
<style>
* { font-family: Arial; }
</style>
<title>HTML div</title>
</head>
<body>
    <p style="margin-bottom: 0in; padding-left: 60px;">
    <div style="font-size: 450%; text-indent: 150px;">
        <strong>BUY <span style="color: #ff420e;">2</span> GET
        </strong>
    </div>
    </p>
    <div
        style="float: left; display: inline-block; margin: 10px; text-align: right; font-size: 70%; line-height: 27; transform: rotate(270deg);">Offer
        Expiry Date : 30/11/2017</Div>
    <div
        style="float: left; display: inline-block; margin: 10px; text-align: right; font-size: 350%;">
        ₹
        <!-- &#x20b9; -->
    </div>
    <div
        style="float: left; display: inline-block; margin: auto; font-size: 1500%; color: red; font-weight: bold;">99</div>
    <div
        style="float: left; display: inline-block; margin: 10px; text-align: left; font-size: 250%; line-height: 10;">OFF</div>
    <div
        style="position: absolute; height: 40px; font-size: 250%; line-height: 600px; color: red; text-indent: 50px">Pepsi
        2.25 Pet Bottle ltr</div>
    <div
        style="position: absolute; height: 40px; font-size: 245%; line-height: 694px; text-indent: 50px">
        MRP: &#x20b9; <span style="color: #ff420e;">654</span>
    </div>
</body>
</html>

Java代码:

public class Test {

    final static String DEST = "D://Workspace_1574973//POP//sample_12.pdf";
    final static String SRC = "D://Workspace_1574973//POP//src//com//resources//test.html";

    public static void main(String[] args) throws Exception {
        createPdf(SRC, DEST);

    }

    public static void createPdf(String src, String dest) throws IOException {

        HtmlConverter.convertToPdf(new File(src), new File(dest));
    }

}

较早的代码,正在使用符号。

            log.info("Creating file start");
            OutputStream file = new FileOutputStream(new File("font_check.pdf"));
            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.getInstance(document, file);
            document.open();

            InputStream is = new ByteArrayInputStream(fileTemplate.getBytes());
            XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
            document.close();
            file.close();

            log.info("Creating file end");

是否有任何简单的方法可以用最少的优化代码来实现这一目标?因为我必须一次性生成数千个pdf,所以性能不应受到影响。如果有人通过最新版本实现了此功能,请告诉我。

编辑:同样如何设置特定的纸张类型,例如A6A3A4

java html css pdf html2pdf
1个回答
1
投票

希望您没有生气,因为我没有写简单评论的名声……所以我将发布完整答案。我为工作解析HTML,有时会读SO。关于UTF-8的话题很多。大多数软件系统都支持“大于char#256”(UTF-8)代码-例如印度卢比符号。但是,大多数时候,程序员必须明确地包括对这种所需行为的特定请求。

例如,在HTML中-添加此行通常有帮助:

String UTF8MetaTag = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";

[无论如何,没有使用HTMLToPDF-我可能不是发布您问题的答案的合适人选-但是,因为我已经处理UTF-8外语字符三年了,所以我知道设置一个软件设置来处理65,000个左右的字符通常非常容易,但是ALWAYS非常强制。

这里是有关使用HTMLToPDF和UTF-8处理日语汉字字符的SO文章。它很可能应该处理所有UTF-8,但这不是保证。

HTML2PDF support for japanese language(utf8) is not working

[这里有几篇有关在PHP中使用HTML2PDF的文章:

Converting html 2 pdf (php) using hebrew returns "???"

Having æøå chars in HTML2PDF charset

© www.soinside.com 2019 - 2024. All rights reserved.