question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.html method can't handle Chinese correctly, even after used the .setFont method.

See original GitHub issue

Hello, I’m using jsPDF 1.5.3 version, but I found Chinese doesn’t display correctly when I use .html method. But it will work when I use the .text method. This makes me confused. So, does there any solutions, for trans a Chinese HTML page to PDF with using the awesome jsPDF? I will be very grateful for this 😊.

Here are my scripts:( I’m using SourceHanSansCN.ttf for Chinese, you can find it from here: Source-Han-TrueType )

<script src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js'></script>
<script src="./js/SourceHanSansCN-Normal-normal.js"></script>

Here is my javascript code:

var doc = new jsPDF({ orientation: 'p', format: 'a4'  });
doc.setFont( 'SourceHanSansCN-Normal', 'normal' );
doc.html( '<div>Chinese: 中文</div>' , {
    callback: function ( newDoc ) {
        newDoc.save( 'chinese-html.pdf' )
    }
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:24

github_iconTop GitHub Comments

5reactions
Uzlopakcommented, May 15, 2019

ok my last post had the html-markup parsed

So you should have to do something like <div style="font-family: SourceHanSansCN-Normal">中文<div>

2reactions
kamranmuazzamcommented, Dec 13, 2019

if doc.setFont(‘fontname’) works then setting font-family:‘fontname’ should work as well. As mentioned earlier by @FaustYKKN you should use lowercase letters while naming your fonts

Read more comments on GitHub >

github_iconTop Results From Across the Web

.html method can't handle Chinese correctly, even after used ...
Hello, I'm using jsPDF 1.5.3 version, but I found Chinese doesn't display correctly when I use .html method. But it will work when...
Read more >
How to make sure Chinese text is rendered in webpage?
All Chinese text is rendered in the same font, so it looks like the browser in some way can find the font that...
Read more >
Different representation of unicode code points in Japanese ...
Generally, CJK characters in Unicode are “unified”, which means that a single code point is used even though the character has traditionally been...
Read more >
TextMesh Pro - Chinese font problems - Unity Forum
I search for a .ttf Chinese font online and import into my project. I then use it to in the Font Asset Creator...
Read more >
Chinese Standard Web Fonts: A Guide to CSS Font Family ...
Good Rules for Using Chinese fonts in CSS. Use the Chinese characters, and also spell out the font name. When declaring a Chinese...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found