boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
733 stars 154 forks source link

Line break is not working #204

Closed cmdr-rohit-bang closed 2 years ago

cmdr-rohit-bang commented 2 years ago

I'm working on inserting a blank page and adding the text into the page. Please help me how to add line break b/w text and auto fit to the page. Thanks in advance.

require 'combine_pdf'
file_text = "Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec rutrum congue leo eget malesuada.\nVestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.\nCurabitur aliquet quam id dui posuere blandit. Nulla porttitor accumsan tincidunt.\nCras ultricies ligula sed magna dictum porta."

pdf = CombinePDF.new
custom_page = pdf.new_page([0, 0, 595.28, 841.89]).textbox file_text, max_font_size: 30, align: 'center', overflow: :shrink_to_fit, font: 'Helvetica'

pdf = Product.last.document.open { |f| CombinePDF.load(f.path) }
pdf >> custom_page
boazsegev commented 2 years ago

Hi @cmdr-rohit-bang ,

Thank you for your interest in CombinePDF.

Questions that are not about bugs / issues would find a better home at stackoverflow.com, where you would get a whole community of experienced developers looking at your question.

Here it is only me and I do not get paid or get any other credit for answering questions. Asking questions on Github is not fair to the open source projects you use.

As a quick note, look into using the textbox method and be aware that PDF documents are not text documents. There's no concept of "new line" in a PDF file, because the PDF wouldn't know the line height or the paragraph margins or where you might like the text to start or end... you draw a new line by splitting the text and using a new textbox... so use file_text.split("\r\n") (or whatever) and handle each line separately, placing it's text in a textbox.

Good Luck.