Answer the question
In order to leave comments, you need to log in
How to generate PDF using custom fonts?
There is a server on NextJS on strict TS and, in addition to the main tasks, it is necessary to send some information in the form of PDF.
And here a problem arises, the pdf is collected, sent and received, but since the fonts are not connected when assembling the html, Times New Roman is obtained everywhere, which, obviously, is not good.
So far I have already tried to solve this problem with:
Answer the question
In order to leave comments, you need to log in
Solved the problem.
So, I returned to html-pdf and the problem was where and how I specified the absolute path and, which is also important, how I specified the path to the fonts in the styled-components package using createGlobalStyle.
Namely, when specifying an absolute path in html-pdf, it must end with "/" (note: " file:///mnt/c/Users/usrname/Documents/repository-f... "), and in createGlobalStyle the path must start with the name of the folder or file (the path is relative, relative to the specified folder in html-pdf), and not with "/" . Otherwise, even if the absolute one is without "/", and the relative one is with "/", fonts will not be loaded (and not only fonts, but everything that is taken from that folder).
Actually, that's all, it remains only to push the svg into PDF...
Upd.
After some time, it was decided to switch to puppeteer, since although html-pdf works, in general, as it should, it does not render html 1 in 1 like a browser, and as a result, one or another graphic bugs had to be fixed with crutches. Moreover, there was a problem with some inconsistency in the display of the width of some letters per pixel, which caused a very noticeable thickening of the letters in the PDF at a distance, but this could not be solved.
So, I didn’t succeed in pushing the fonts into the pupil, so I went the other way: I made 2 different ways for loading and for displaying the PDF, in the first one a page opens in the pupil and a “front” PDF is requested with everything that is needed.
Next, the page is converted to PDF with properly rendered html.
Separately, I note that I had to use Pupiter 1.18.0, in my opinion (or thereabouts), because in more recent versions Pupiter stubbornly returned an empty PDF.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question