jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

utf8 support

Open amarantedaniel opened this issue 5 years ago • 19 comments

Texts don't render correctly depending on what text you write.

doc.text('A • B', 20, 20)
doc.save()

renders þÿA " B

In the original version of jsPDF this is fixed. What are the plans on implementing that?

amarantedaniel avatar Feb 27 '19 12:02 amarantedaniel

Did you add a font supporting these utf8 characters? If yes, could you give us a link to the respective issue/pull request?

HackbrettXXX avatar Feb 27 '19 15:02 HackbrettXXX

I'm using the default font, but the same code works when using MrRio/jsPDF so I assumed it wasn't a font problem

amarantedaniel avatar Feb 27 '19 16:02 amarantedaniel

When I run the code with MrRio/jsPDF, it renders to A " B, which also not correct.

HackbrettXXX avatar Feb 27 '19 17:02 HackbrettXXX

We're having similar problem over here. It happens with many characters, such as , , etc. When using default fonts, it renders þÿ (which is utf16 BOM "cast" to ansi). When using utf8 compatible font, the character gets stripped completely. I'm trying to get to the bottom of this, please let me know if you have any insights!

KenjiTakahashi avatar Jan 07 '20 10:01 KenjiTakahashi

If you think this is a bug, please do provide ways to reproduce the bug. Adding characters outside the ASCII range does not and cannot work with default fonts which only cover the ascii range. Please see the custom font examples, which do work, as far as I can tell.

yGuy avatar Jan 07 '20 12:01 yGuy

Alright, so I got around to it and indeed it is not jspdf problem, sorry! It works fine when using Unicode aware font, but in our real code, jspdf is called by Highcharts and it is removing part of our custom font-family definitions when exporting to PDF (why? no idea).

Still, I have to say that the font situation here is complicated. I don't know if there's any improvement that can be made, though. Maybe even stating explicitly in the readme/docs that default fonts are not Unicode aware would be nice?

KenjiTakahashi avatar Jan 10 '20 09:01 KenjiTakahashi

@KenjiTakahashi - I'm facing a similar challenge with local exporting of Highcharts where I need unicode symbols.

I've converted a unicode aware font Lucida Sans Unicode font (named l_10646) using the jsPDF fontconverter and included it. Then, I modified the highcharts code to call: pdf.setFont("l_10646") before it calls win.svg2pdf.

But still the exported PDF shows garbled unicode characters. Can you share your approach and how you fixed it?

Jollyfant avatar Mar 22 '20 12:03 Jollyfant

Did you load the font file after jsPDF?

HackbrettXXX avatar Mar 23 '20 09:03 HackbrettXXX

Yep and when checking with e.g. pdf.__private__getFont("l_10646") it exists. So maybe there are some extra settings with Highcharts that need to be modified and @KenjiTakahashi may have the answer.

Jollyfant avatar Mar 24 '20 10:03 Jollyfant

Does it work if you manually add text to the pdf with this font?

HackbrettXXX avatar Mar 24 '20 12:03 HackbrettXXX

Yep, good idea and that works! The encoding is fine and the symbols are there. Highcharts makes a call to svg2pdf so perhaps the problem is there.

win.svg2pdf(svgElement, pdf, { removeInvalid: true });

Jollyfant avatar Mar 24 '20 16:03 Jollyfant

What does the SVG look like that's passed to svg2pdf? Does it use your custom font?

HackbrettXXX avatar Mar 25 '20 08:03 HackbrettXXX

First thing I'd do is to use some pdf editor (I use masterpdfeditor) and check if the text is actually using your font. Like I mentioned, highcharts is doing funky stuff with font-family declarations, so if it is not being used, make sure you have only 1 entry in font-family for your svg element(s) and see if it helps.

KenjiTakahashi avatar Mar 28 '20 22:03 KenjiTakahashi

Thanks for the suggestions and after some hacking it works now. It may very well be a problem with highcharts: https://github.com/highcharts/highcharts/issues/6417#issuecomment-605987987.

Jollyfant avatar Mar 30 '20 13:03 Jollyfant

Hey there! I have the same issue using your fork of jsPDF. The custom font is Inter-bold.ttf (converted using the official base64 tool) and loaded with an own script tag just below the CDN link of your library.

It is shown correctly in the browser but once you download the file as PDF and not open it in the browser, all glyphs are messed up.

I use it like this:

doc.setFont('Inter-Bold', 'bold');
doc.text(0, 0, 'Hello');

You can reproduce the issue here: betastage.netlify.app And here's the corresponding repo: https://github.com/kilianso/betastage - All jsPDF related code can be found in public/index.html and src/App.svelte

kilianso avatar Jul 27 '20 07:07 kilianso

Actually looks pixel-perfect for me when viewing on Chrome/Edge - what PDF viewer are you using?

yGuy avatar Jul 27 '20 07:07 yGuy

Please read again. It is showing correctly in the browser but not in the actual PDF if you open it not in the browser. PDF viewer is the built in "Preview" app of MacOS.

By the way, adding the font like this:

doc.addFont('fonts/Inter-Bold.ttf', 'Inter-Bold', 'bold')

is not working either.

And i tried different other fonts. All with the same result.

kilianso avatar Jul 27 '20 08:07 kilianso

The PDF looks good to me, as well. It correctly contains the font and looks good in Chrome and in Adobe Reader. You probably ran into https://github.com/MrRio/jsPDF/issues/2743. Try to disable compression as a workaround.

HackbrettXXX avatar Jul 27 '20 08:07 HackbrettXXX

@HackbrettXXX Ahh nice! Indeed setting compression to false resolves the issue. Would be cool to mention this in the documentation. Thanks a lot.

kilianso avatar Jul 27 '20 08:07 kilianso