Pdfkit Fonts

0 views
Skip to first unread message

Niklas Terki

unread,
Aug 5, 2024, 12:39:09 AM8/5/24
to sapdeciwyc
InternallyPDFKit keeps track of the current X and Y position of text as itis added to the document. This way, subsequent calls to the text method willautomatically appear as new lines below the previous line. However, you canmodify the position of text by passing X and Y coordinates to the textmethod after the text itself.

PDFKit includes support for line wrapping out of the box! If no options aregiven, text is automatically wrapped within the page margins and placed in thedocument flow below any previous text, or at the top of the page. PDFKitautomatically inserts new pages as necessary so you don't have to worry aboutdoing that for long pieces of text. PDFKit can also automatically wrap textinto multiple columns.


The text will automatically wrap unless you set the lineBreak option to false.By default it will wrap to the page margin, but the width option allowsyou to set a different width the text should be wrapped to.If you set the height option, the text will be clipped to the number oflines that can fit in that height.


When line wrapping is enabled, you can choose a text justification. There arefour options: left (the default), center, right, and justify. Theywork just as they do in your favorite word processor, but here is an exampleshowing their use in a text box.


If you're working with documents that require precise layout, you may need to know thesize of a piece of text. PDFKit has two methods to achieve this: widthOfString(text, options)and heightOfString(text, options). Both methods use the same options described in theText styling section, and take into account the eventual line wrapping.


The list method creates a bulleted list. It accepts as arguments an array of strings,and the optional x, y position. You can create complex multilevel lists by using nested arrays.Lists use the following additional options:


As mentioned above, PDFKit supports a simple form of rich text via the continued option.When set to true, PDFKit will retain the text wrapping state between text calls. This way,when you call text again after changing the text styles, the wrapping will continue rightwhere it left off.


The options given to the first text call are also retained for subsequent calls after acontinued one, but of course you can override them. In the following example, the widthoption from the first text call is retained by the second call.


The PDF format defines 14 standard fonts that can be used in PDF documents. PDFKit supports each of them out of the box.Besides Symbol and Zapf Dingbats this includes 4 styles (regular, bold, italic/oblique, bold+italic) of Helvetica,Courier, and Times. To switch between standard fonts, call the font method with the corresponding Label:


To change the font used to render text, just call the font method. If youare using a standard PDF font, just pass the name to the font method.Otherwise, pass the path to the font file, or a Buffer containing the font data.If the font is a collection font (.ttc and .dfont files), meaning that itcontains multiple styles in the same file, you should pass the name of the styleto be extracted from the collection.


I would like to discuss here if anyone else is noticing that some PDF files are not rendered as expected in iOS/iPadOS 17, it seems that some text with background (screenshot attached) are not rendered and you can only see the back color.


I have similar issue with the PDFKit after the iOS 17 update. I cannot, for the life of me, understand how such unstable releases are made by apple. In my app, many views are rendered into a pdf for export. This has completely stopped working after several years of being in use. Any hints to why there are issue here would be very useful.


I have similar issue, on iOS 16 and previous PDF rendering was working pretty fine.After upgrade to iOS 17+ (issue is also replicating on iOS 17.1.1), some specific fonts on PDFs are displaying as black boxes (when the font color is set to black).


I'm using python 3.6 with pdfkit 0.6.1 (and it seems wkhtmltopdf 0.12.3.2) on a Debian Docker image. I tried looking at the docs & wkhtmltopdf options but there's no way to specify the font for the whole document. There are only font options for footers & headers.


I have managed to customize the font of my entire document passing a CSS stylesheet using user-style-sheet. Inside your CSS file, in order to avoid issues with the font structure, I recommend you to convert the font to a base64 format. base64 reference


I use pdfkit to generate PDF document from the HTML page. The problem is that it uses different fonts on my development machine (OS X) and production server (Ubuntu), so I can't get consistent rendering on development and production environment.


I have tried several different methods but most help seems to be suggesting CSS which I don't see how I can use here. Unfortunately I am unable to upload a font to my own filesystem to use as the site where I am building it blocks upload of fonts for some reason. This is why I went with the google fonts as I didn't run into CORS issues.


I directly linked to the woff2 file as getting an arraybuffer using any of the other methods google recommends doesn't seem to work. As a note I will be using both Latin and Cyrillic characters so this might be an issue since they are separate character sets it appears


We just had the same problem with wicked_pdf, which uses wkhtmltopdf, too. Our solution was to download the font and install them on the machines, which generate the pdfs. Then use the fonts like every other system font.


I am using nodejs pdfkit to generate a pdf. I want to be able to bold or italic individual words in a line. It looks like pdfkit doesn't support this, so I was wondering if anyone had done something similar?


Please look at the fiddle example. I am less knowledge on working with pdf fonts. I am using pdfkit to generate the pdf.I am trying to load the japanese fonts but no luck. I followed most of the articles from stackoverflow but i am not successful in writing japanese text to the pdf.


I find that Preview display of text in PDF files is rather sloppy, I am pretty sure it used to be much sharper in Mavericks. At the very least now Preview is significantly less sharp than Acrobat Reader, you can check out a screen-shot to convince yourself of what I am talking about.


In the rest of the UI I am quite happy with how fonts are displayed. So I was wondering if there are ways to control the anti-aliasing of Preview. In the preferences of Preview I can find only a global switch for anti-aliasing, but I would like to play around with the strength of the anti-aliasing so to find the optimal setting.


I am looking into defaults of com.apple.Preview but in my current values there seem to be no setting about Font Smoothing or anything evocative of anti-aliasing. Any suggestion about possible values to edit here?


I have found a solution to this problem: replacing Yosemite's PDFKit with Mavericks' PDFKit. As simple as that. I replaced the contents of /System/Library/Frameworks/Quartz.framework/Versions/A/Frameworks/PDFKit.framework/Versions/A and everything is working fine now.


For those who don't have a copy of Mavericks from where you can take the PDFKit files, I have uploaded a copy to: (hope there are no copyright issues with this - I will remove the link if asked to do so).


By the way, changing the Current symlink to point to the Mavericks version and keeping the original A/ directory DIDN'T fix the issue. I had to remove the Yosemite version completely and put the Mavericks version contents inside A/ (it seems Preview and Skim both are hard coded to Versions/A and do not follow the Current symlink).


Apparently in Yosemite, Preview doesn't use LCD font smoothing (subpixel rendering) even if LCD font smoothing is enabled from System Preferences. Skim never uses LCD font smoothing either, but the way text is rendered depends on the AppleFontSmoothing setting or whether LCD font smoothing is enabled from System Preferences.


Commands like defaults write -app Preview AppleFontSmoothing -int 2 don't have any effect. I didn't find any preference keys by running strings /Applications/Preview.app/Contents/MacOS/Preview or strings /System/Library/Frameworks/Quartz.framework/Versions/A/Frameworks/PDFKit.framework/PDFKit either.


When I use pdfkit to generate a PDF from HTML on Pythonanywhere and then have fitz read the text out of the produced PDF, I get replacement characters for all the spaces. When I run the exact same code in my home environment, I get the accurate spaces. Here is the code I'm running.


I) the sum of the amount on which tax is determined under subparagraph (A) plus the net capital gain, over(II) taxable income; and(F) 28 percent of the amount of taxable income in excess of the sum of the amounts on which tax is determined under thepreceding subparagraphs of this paragraph.


When I create the PDF on Pythonanywhere and read it in my home environment, I get the same text with the replacement characters, so the fitz part seems to be working the same in both environments and the issue seems to be with the generation of the PDF. Additionally, the PDF looks fine whether I generate it in my home environment or on Pythonanywhere; the issue is apparent only when I extract the text back out from the PDF.


To check if it was the font, I took the font specification out of the CSS entirely on PythonAnywhere, presuming that it would use whatever the default font was, which I think was DejaVuSerif. It did appear to be using DejaVuSerif, but I had the same problem (replacement characters) with that font on PythonAnywhere.


I noticed something else, which is that the size of the font differs between running it on my home environment and running it on PythonAnywhere. It is much smaller on my home environment. To get roughly the same font size, I have to put 'minimum-font-size': '40' in the pdfkit options for my home environment, and 'minimum-font-size': '22' for the PythonAnywhere environment. That also may suggest that it's something about fonts. Is there another way I could try a different font that you would suggest (given that I have already tried taking font specification out of the CSS file entirely)? Thank you very much for your help with unraveling this.

3a8082e126
Reply all
Reply to author
Forward
0 new messages