![]() ![]() Use the size property to specify a string which is the name of the paper size. To use any of these predefined page sizes, pass an object to either the PDFDocument constructor or the addPage() method. The PDFKit text documentation has a complete list of these standard fonts. PDFKit supports each of the 14 standard fonts defined by the PDF format. To move up or down a line, call the moveDown or moveUp method with the number of lines the text should move: doc.moveDown( 3) īy default, calling either method without passing an argument will cause it to move by just a single line. However, by providing X and Y coordinates to the text method after the text itself, you can change its position: doc.text( 'Coding is Easy!', 100, 100) So, whenever you call the text method, PDFKit will add a new line beneath the previous one. You can set the page margins in two ways. The first is by setting the margin property (singular) to a number, which applies that margin to all edges. Passing a page options object to the PDFDocument constructor will set the default paper size and layout for every page in the document, which is then overridden by individual options passed to the addPage method. To add text to the document, simply call the text() method and pass the text that you want to add as the argument: doc.text( "Coding is Easy!")Īs PDFKIT adds text to the document, it keeps track of that text's current X and Y positions. You can set the page margins in two ways. PDFKit makes adding text to documents simple and includes many options to customize the display of the output. You only have to ensure that the element you use for "representing" a page ( wrapper in this example) has the same dimensions as the page's.Before you pipe your document to save it, you can make various changes. * The selector applies the page break after, so we have to exclude the last wrapper */Īnd voila, you have a perfectly fitted template for rendering PDF data, with no magic spacing. Height: 160mm /* + 50 from vertical padding = 210 */ Width: 197mm /* + 100 from horizontal padding = 297 */ * To ensure no unexpected magic spacing */ Taking that into account, an example would be this: It secures that any magic spacing isn't setted when printing the HTML (via render, via web browser). The good news are that not setting any margin/padding/positioning will not break things, so in some stuff it'll be good enough.īut what if you need to add margins or something like that? CSS's page-break properties (the linked one is page-break-after) comes to the rescue. So that means that adding any type of margin/padding/positioning will break things and create magic spacing, as the lengths aren't correct (even if you use the same dimension unit for those). The problem gets when PDFKit, that it uses WebKit from what I know, renders HTML: WebKit only has a 3 decimal precision point value, so our 3.779528 pass to be 3.779 (it rounds towards down if I remember), which is not the real size. ![]() So, translating it into px (because its easier to set cm or mm instead of calculating manually pixels), we have that 1 mm = 3.779528 px. Let's say I want to print a paper size A4 (you have a list here). Ok, I finally managed to understand what was happening: The obvious (ie, easy) solution is to change the actual 'page size' in the PDFView and have wider margins, but of course PDFKit has no support for that (I dont think). (Note: yeah, page-height is 1mm larger than height in HTML, but that is needed so the wrapper fits in the PDF page).Īnyone had meet this problem before? Thanks in advance. ' create a new document Dim document As New Document() ' append a new page (portrait Letter) Dim landscape As Boolean False Dim page As New Page(PageSize.Letter, landscape) (page) ' stamp a text paragraph at the center Dim multilineText As New MultilineTextShape() Dim translate As New TranslateTransform() multilineText.Transform translate Dim fragment As New Fragment(text. I tried different combinations of margins, paddings and heights and nothing: when I just add margins or paddings, the PDF gets crazy. I generate the PDF, and looks great (no surprises, all goes where it belongs).īut, if I add any margin/padding in the HTML and/or options, I just get a continued repetitive incremental margin when the PDF is generated (to give an example, in 50 pages, the first one has no margin, and the 50th has a margin of about two fingers more or less). In both cases I just don't add any margins or paddings. I'm using PDFKit with Python 2.7 and Django 1.9 to generate some HTML to PDF. Link for similar post would be thanked if founded, as I didn't found one. Although I just don't have the need to ask something normally (internet is just a gold mine, especially this site), this is getting me off, as I had been two days trying to get this working and I don't find anything useful. ![]()
0 Comments
Leave a Reply. |