printpdf
printpdf copied to clipboard
Multiline strings
I love this library it's easy to use and understand, perfect. The only issue I'm running into currently is multiline strings e.g.
let text_source_address = "610 Monroe St.\nStoudsburg, PA 18630-2115";
The output from:
current_layer.use_text(text_source_address, 10, Mm(30.0), Mm(230.0), &font);
is:
610 Monroe St. Stoudsburg, PA 18630-2115
Ideas?
Thank you for your efforts
Use the add_line_break
function:
let text_source_address = "610 Monroe St.\nStoudsburg, PA 18630-2115";
pdf_layer.begin_text_section();
pdf_layer.set_font(&font, 10);
pdf_layer.set_text_cursor(Mm(30.0), Mm(230.0));
for line in text_source_address.lines() {
pdf_layer.write_text(line, &font);
pdf_layer.add_line_break(); // <---
}
pdf_layer.end_text_section() // <- important!
If you take a look at the source code of use_text
, it does just that:
Maybe I should add this to the use_text
function.
Thank you for the reply, and it works with one little issue, all of the lines are on top of each other?
I assume that the set_text_cursor, sets the placement of the first text line, but the other lines get placed on top of the first so should I add a new text cursor with each line break?
Sorry to be a pain and thank you for your time in helping me out.
Ashley
I think I forgot that you need to call set_line_height(10)
first so that the PDF knows how much to advance. If that doesn't work, you have to calculate the (x, y) position of the line manually.
let text_source_address = "610 Monroe St.\nStoudsburg, PA 18630-2115";
pdf_layer.begin_text_section();
pdf_layer.set_font(&font, 10);
pdf_layer.set_text_cursor(Mm(30.0), Mm(230.0));
pdf_layer.set_line_height(10); // <- same as font size (or more, if you want more spacing)
for line in text_source_address.lines() {
pdf_layer.write_text(line, &font);
pdf_layer.add_line_break(); // <---
}
pdf_layer.end_text_section() // <- important!
EDIT: Yeah, I even added this to the documentation, lol:
Perfect, it works great.
Thank you for your time and effort, I'm a happy camper.