Search code examples
javascriptcharacter-encodingjspdf

Why does "Č" symbol makes my PDF completely weird?


I use jsPDF in order to generate PDFs. When I use french or english letters, there is no problem. But once I use slovenian language, jsPDF generates weird PDF. I know it has to do with encoding, but I tried many and can't figure which one to take. Here are 2 pictures of what it looks like in english and in slovenian :

English version PDF Slovenian version PDF

The english string is : "Power cut" and the slovenian string is : "Izpad električne energije". I think there is no need of my code here. I just have to understand how to correct the issue.


Solution

  • Reminder to self and others the behavior will be odd if not saving the HTML in an editor as UTF-8 with BOM (ByteOrderMark) , this tripped me up on a few tests.

    The main instructions can be found at https://github.com/parallax/jsPDF#use-of-unicode-characters--utf-8

    enter image description here

        <!DOCTYPE html>
        <html lang=pl>
          <head>
            <title>Title of the document</title>
          </head>
          <body>
    
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/" crossorigin="anonymous"></script>
        <script>
        var doc = new jsPDF({
          orientation: 'p',
          unit: 'mm',
          format: 'a4'
        })
    
        window.onload = function fontDemo() {
        const NotoSans = 'AAEAAAANAIAAAwBQR0RFRvDX3ecAA8DgAAAFwEdQT1OembooAAPGoAAA6hhHU1VCRDfqagAEsLgAABaAT1M
    

    ... too long for a snippet ...

    gEUARYBGAEaARwBHgEgASIBJAEmASgBKgEsAS4BMAEyATQBNgE4ATsBPQE/AUMBRQFHAUkB+gH8Af4CAAITAhQCFQNhC4ELgguDC4QLhQuGC4cLiAuJC4oLiwunAAEAAAABAAgAAgBCAB4J7wnwCfEJ8gnzCfQJ9Qn2CfcJ+AnvCfAJ8QnyCfMJ9An1CfYJ9wn4Ce8J8AnxCfIJ8wn0CfUJ9gn3CfgAAgADABMAHAAACfkKAgAKCgQKDQAUAAEAAAABAAgAAQAGAbwAAQABCtcAAQAAAAEACAABAAYJ8AABAAEAEw==';
    
        doc.addFileToVFS("NotoSans.ttf", NotoSans);
        doc.addFont("NotoSans.ttf", "NotoSans", "normal");
        doc.setFont("NotoSans");
    
        doc.setTextColor(0, 0, 1);
        doc.setFontSize(50);
        //  text, x, y, var, var, 'alignment'
        doc.text("Izpad električne energije", 105,  100, null, null, 'center');
        doc.save("Example.pdf");
        }
          </script>
          </body>
        </html>
    

    File: Example.pdf
    Created: 7/25/2022
    PDF Producer: jsPDF 1.5.3
    PDF Version: 1.3
    File Size: 69.79 KB (71,460 Bytes)
    Number of Pages: 1
    Page Size: 8.27 x 11.69 in (A4)
       
    Fonts: Courier (Type1; Ansi)
    Courier-Bold (Type1; Ansi)
    Courier-BoldOblique (Type1; Ansi)
    Courier-Oblique (Type1; Ansi)
    Helvetica (Type1; Ansi)
    Helvetica-Bold (Type1; Ansi)
    Helvetica-BoldOblique (Type1; Ansi)
    Helvetica-Oblique (Type1; 
    

    NotoSans (TrueType (CID); Identity-H; embedded)

    Symbol (Type1)
    Times-Bold (Type1; Ansi)
    Times-BoldItalic (Type1; Ansi)
    Times-Italic (Type1; Ansi)
    Times-Roman (Type1; Ansi)
    ZapfDingbats (Type1)