0

I updated my code from iText 5.0 to iText 7 and html2pdf 2.0 according to this post. In earlier version rupee symbol was rendering properly, but because of css issue i changed the code. Now complete page is converting properly to pdf except rupee symbol.

  1. Tried adding font in html style tag itself like * { font-family: Arial; }.
  2. Changed value of rupee symbol from &#x20b9, ₹ and also added directly ₹ , but no use.

My 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 Code :

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

}

Earlier code, which was working with symbols.

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

Is there any simple approach to achieve this, with minimal and optimized code ? Because I've to generate thousands of pdf in one go, So the performance should not affect. Please let me know, if anyone achieved this through latest version.

Edit : Also how to set particular paper type in this like A6, A3, A4 etc.

Amol Bais
  • 332
  • 1
  • 6
  • 30

1 Answers1

1

Hope you are not mad, because I don't have reputation to write simple comments... so I'll post a full answer instead. I parse HTML for my work, and I read SO sometimes. There is a lot on the subject regarding UTF-8 here. Most software systems support the "greater than char #256" (UTF-8) codes - for instance the Indian Rupee Symbol. However, most of the time the programmer has to include a specific request for such a desired behavior, explicitly.

In HTML, for instance - adding this line usually helps:

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

Anyway, not having used HTMLToPDF - I might not be the right guy to post answers to your questions - but, because I have dealt with UTF-8 foreign language characters for three years, I know that setting a software setting to handle the 65,000 or so chars is usually VERY EASY, BUT ALSO ALWAYS VERY MANDATORY.

Here is an SO post about using HTMLToPDF and UTF-8 to handle Japanese Kanji characters. Most likely, it should handle all UTF-8, but that is not a guarantee.

HTML2PDF support for japanese language(utf8) is not working

Here are a few posts about it using HTML2PDF in PHP:

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

Having æøå chars in HTML2PDF charset

  • the question is about itext7 , the char related thing i already achieved in itext5, but the same charset is not working with higher version – Amol Bais Oct 04 '19 at 05:55
  • ? ? - Here's the title written on your post: "Simple way to display rupess symbol in html2pdf" ... Anyway, I was bringing up how "higher order" UTF-8 chars have worked in the software environments that I have programmed... Mentioning it because I program Mandarin Chinese (and all the symbols required)... It has always been (for me) finding how to set for allowing UTF-8... I also program Spanish too - and it's the same. –  Oct 04 '19 at 11:54
  • UTF-8 encoding worked perfectly for iText version 5, the same encoding and same html page design is not working with higher version of iText (version 7). Anyway, i found workaround by using currency image symbol :-( – Amol Bais Oct 09 '19 at 14:27