How to add line break in pdf generated using pdfkit.from_string()?
Asked Answered
E

1

6

I try to generate a pdf file from a string using pdfkit. However, in the result all new line characters \n are ignored. Thus, I get a pdf with a string printed as a single sentence. For example:

import pdfkit
s = "Some text\n"
s += "Ok, let's add something more\n"
s += "And more..."
pdfkit.from_string(s,"file.pdf",{'encoding':'utf-8'})

Following code results in a following output (meaning a pdf file with following text):

Some text Ok, let's add something more And more...

However, this is what I want to achieve:

Some text

Ok, let's add something more

And more...

Escort answered 3/6, 2020 at 13:8 Comment(0)
C
0

you can replace the '\n' with a moveDown() operation in javascript so far.

let doc = new PDFDocument({ size: "A4", margin: 50 });
doc.text("Some text")
   .moveDown()
   .text("Ok, let's add something more")
   .moveDown()
   .text("And more...");

doc.end();
const path = "/Users/user1234/Documents/stackoverflow.pdf"
doc.pipe(fs.createWriteStream(path));

For strings which content you don't know, you can write a parser that also executes the moveDown() operation once it sees a linebreak.

Cudgel answered 19/10, 2022 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.