This Ruby utility replaces placeholders in a .docx
file with provided values and converts the result to a PDF using LibreOffice.
- Replace placeholders like
{{name}}
,{{date}}
, etc. in a Word document. - Generate a filled PDF version of the document.
- Temporary files are managed in the system temp directory.
- Safe file generation using
SecureRandom
.
- Ruby (>= 2.5)
- LibreOffice installed and available as
soffice
in the system path - Gems:
docx
securerandom
Add the following line to your application's Gemfile:
gem 'docx'
gem 'word_to_pdf'
And then execute:
bundle install
Or install it yourself as:
gem install docx
first create a template sample word file(.docx)
This is test file
Name: {{name}}
date of birth: {{date}}
or \
You can download a sample .docx
template to test this gem:
👉 Click here to get the sample file
require 'word_to_pdf'
WordToPdf.convert(
'/path/to/template.docx',
'/path/to/output.pdf',
{
first_name: 'Test',
last_name: 'Example'
}
)
You will have the downloaded file on the /path/to/output.pdf
location