-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert html to pdf: utf-8 works only with text, but not html (unicode/cyrillic not working) #2968
Comments
Please try setting the font name also in the html markup. E.g.
|
not working
|
Then I guess it's a bug... |
@HackbrettXXX the same with Polish signs :( I'm tring to create pdf in this way:
|
It works? |
@potapovnikita as I mentioned - there is a problem. I just showed part of my code in case I made a mistake somewhere :) |
I would like to mention two things here.
so, after spending almost my entire day on it, I have figure out the below solution which helps me to get those bullets on my pdf. after adding custom fonts just like explained below article you have to import your font files into your component and just style your HTML like this. `import '../../../Lato-Regular-normal'; const htmlToConvert = doc.html(htmlToConvert,{ |
Hope that it will help some one. |
So, I guess it's not a bug but a lack of clarity for the use case. |
In my case, using the version 2.1, setting the font style on the html element like so: |
@potapovnikita which jsPDF version do you use? If it's not the current one please update. @Rui-Jesus If this only works if the markup is passed as element, this might be an encoding issue of the file that contains the html markup? |
It works, and it works wonderfully, thank you <3 |
@HackbrettXXX I did not test passing a file with the html markup, I had it stored in memory, the html markup. To be honest I didn't look too much into the issue since the solution I provided works perfectly for me. |
Thank you. He inspired me. This is the end result: const htmlStr: HTMLElement = document.querySelector('.main') || document.body;
doc.html(htmlStr, {
callback(isPdf) {
// add the font to jsPDF
doc.addFileToVFS('fzjt.ttf', MyFont); // addfont
doc.addFont('fzjt.ttf', 'MyFont', 'normal');
doc.setFont('MyFont');
isPdf.text('又是中文', 10, 10);
isPdf.save();
},
}); .main
background #ffffff
width 90%
font-family 'MyFont' It's amazing 🎉 |
I have a same problem. utf-8 characters not working with html method. if I try only generate: |
I am building a web app with three cultures
Then I did few modifications to make it work with specific font (the same process didn't work with all fonts)
<!-- GoogleFonts: Cairo -->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Cairo&display=swap" rel="stylesheet">
body {
font-family: 'Cairo', sans-serif !important;
}
// this.addFont('Cairo-Regular-normal.ttf', 'Cairo-Regular', 'normal');
this.addFont('Cairo-Regular-normal.ttf', 'Cairo', 'normal');
<body style="font-family: 'Cairo', sans-serif !important;">
<!-- jsPDF -->
<script src="~/lib/jspdf/dist/jspdf.umd.min.js"></script>
<script src="~/js/html2canvas.min.js"></script>
<script src="~/lib/dompurify/dist/purify.min.js"></script>
<script src="~/js/Cairo-Regular-normal.js"></script>
<script>
$(function () {
setTimeout(function () {
window.jsPDF = window.jspdf.jsPDF;
// Default export is a4 paper, portrait, using millimeters for units
const doc = new jsPDF('p', 'pt', 'a4');
doc.html(document.body, {
html2canvas: {
scale: 0.5,
scrollY:0
},
x: 0,
y: 0,
callback: function (doc) {
window.open(doc.output('bloburl'));
}
});
}, 1000);
});
</script> Now I have a working solution with English and Turkish : But it is not fully supporting Arabic. Some letters are missing, and if I have multiple languages the latin letters are rendred over each other as seen in the below image: The correct html page looks like below: I also tried with adding |
my solution to this problem:
|
I have the same problem. so it is my code but not supporting fornAwesome and cyrylic.
|
just cyrylic text correct displaying but by document element not supported
|
also I can't set HTML as just string to doc.html():
|
any progress on this issue? |
Well, dear friends, perhaps I have found the Main Evil. To display .html with a specific font as of October 05, 2021, it is enough to specify the font as a style for the html element, and it will be applied to all the text inside it. It is important to use the correct fonts, which have the layout of your alphabet, Cyrillic, Polish, etc. This is my experience of 2 days searching for an answer to my problem. Hope this helps someone not to waste time. Alternatively, write the same set of fonts that is used on your site using the |
@NeonDB not working for me. I'm using Roboto font on page. And this initialization is not working: |
This cannot work, since the html markup source is passed as the first property to the .html method. You are not explicitly specifying which part of the html code you are going to use, be it body, div.container_kek, etc. Fix that and this piece of code should work correctly. I also recommend getting rid of "var" by replacing it with "let", it's prettier :) If you are using the same font on the page, you may not need to use the .addFont & .setFont methods. Importantly, different styles, such as Bold, Italic are also a type of font, and they also need to be specified. I forgot an important point, this is adding a font to the jsPDF API, for this use this link, https://rawgit.com/MrRio/jsPDF/master/fontconverter/fontconverter.html load your font in the .ttf format into it and paste the resulting code into yours. |
@NeonDB what do you mean by "not explicitly specifying" this is what i see on webpage: and this is how i see it on generated PDF: jsPDF can't generate characters: ľ č ť i have converted Robot font and attach to code |
thank you so much |
My solution is:
const doc = new jsPDF({
orientation: "portrait",
format: "a4",
unit: "px",
hotfixes: ["px_scaling"],
});
doc.addFileToVFS("Roboto.ttf", RobotoStringBase64);
doc.addFont("Roboto.ttf", "Roboto", "normal");
doc.setFont("Roboto");
await doc.html(
`<html>
<body >
<div style="width: 210mm;">${element.innerHTML}</div>
</body>
</html>`,
{
callback(doc) {
window.open(URL.createObjectURL(doc.output("blob")));
},
margin: [30, 15, 30, 15],
},
) |
Thanks it work for me! |
It works for me, if use div tag instead html |
I've tried this and it worked, except the text rendered was a bit overlapped. |
Same problem as @jedaix. I got and HTML object that cannot be converted to string. I try setting the style of the HTML object, but it still doesn't want to output characters čđć. I get my HTML object from ReactQuill. |
Hi guys. I figured out the reason. It's about html2canvas. The fact is that if your styles explicitly specify the font, and it is not imported into the jspdf fonts, then you will not display correctly. When the html2canvas library reads the css properties from your html, it takes into account the css font-family as well. Example of non-working code:
Now remove the css property:
And now everything works!!! |
You need to remove fonts from css or import them into jspdf. See my comment above |
You need to remove fonts from css or import them into jspdf. See my comment above |
You need to remove fonts from css or import them into jspdf. See my comment above |
After 4 hours of intense debugging this worked for me: jsPDF version 2.5.1 tested for symbols:
Hope this helps some of yall 🙏 |
None of the examples on this page worked for me, neither for Chinese nor for Cyrillic symbols. I'm using jsPDF version 2.5.1 and Arial Unicode MS font. Are there any plans to fix this problem? |
POLISH LETTERS Here is my working js fiddle (multipage) - I left it here for future readers |
It works with some fonts, but not with all - that is the first problem one will have if trying to work with this (for example, it doesn't work with Roboto). Next, I was able to create a regular font (like you did) but when I start adding italic, bold, and bold italic everything was messed up again. Simply said it can't give you the full thing anyway. So what I did I dismiss everything and just replace ČĆĐ (čćđ) to c i dj (what in Serbian Latin script we can read without any problems). Of course, that may not work for more complex scripts, but my client is happy! |
I've finally got it working. Here is my piece of code: `
` A few important things are:
|
thank you so much, i downloaded custom font, and I had to add style="font-family: ..." to wrapper div |
Steps to Follow for Exporting HTML Content with Cyrillic Fonts: 1.Download Roboto Font: 2.Add Roboto-Regular Font: 3.Import Font in Code:
|
use it like this
|
Setting PTSans-Bold on body worked for me import { jsPDF } from 'jspdf'
import '@/assets/font/PTSans-Bold-normal'
export const useDownloadCompPdf = () => {
const savePDF = (htmlElement: HTMLElement) => {
const body = document.querySelector('body')
body.style = 'font-family: PTSans-Bold'
const doc = new jsPDF({
orientation: 'l',
unit: 'mm',
format: 'b0',
putOnlyUsedFonts: true,
floatPrecision: 'smart',
})
doc.addFont('PTSans-Bold-normal.ttf', 'PTSans-Bold', 'normal')
doc.setFont('PTSans-Bold')
doc.html(htmlElement, {
callback(pdf) {
pdf.addFont('PTSans-Bold-normal.ttf', 'PTSans-Bold', 'normal')
pdf.setFont('PTSans-Bold')
pdf.save('invoice.pdf')
},
})
setTimeout(() => {
body.style = 'font-family: Inter, sans-serif'
}, 1000)
}
return { savePDF }
} |
I managed to get multiple different fonts working for me. I'll try to map out my steps (a lot of it is based on above advice).
|
Works like a charm! THANKS. I was so close but was really messing up the matching names and classes in my less files. Kept getting me frustrated but going step by step as you said worked perfectly. |
|
@stif4 thank you so much! Your answer was the last, but its finally works for me! 🙌🏻 |
I'm opening this issue again.
I need to convert html to pdf with jsPDF. It contains UTF-8 symbols (cyrillic). I used fontconverter to generate js-file for my custom font as written here: https://github.com/MrRio/jsPDF
So now example with text works like a charm (from https://github.com/MrRio/jsPDF/blob/master/examples/js/russian.js)
And example with html ignores my custom font and exports incorrect symbols.
What I need to do to export html to pdf with unicode symbols?
The text was updated successfully, but these errors were encountered: