I’m creating a services to generate or modify a docx file with some data from my database this is the code:
require 'docx'
require 'prawn'
class DocumentGenerator
TEMPLATE_PATH = Rails.root.join('app', 'assets', 'docs_templates', 'plantilla_word.docx')
def initialize(dni)
@dni = dni
end
def generate_docx
byebug
if File.exist?(TEMPLATE_PATH)
doc = Docx::Document.open(TEMPLATE_PATH)
doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
puts bookmark_name
end
byebug
# doc.bookmarks['first_lastname'].replace(@dni.first_lastname)
# doc.bookmarks['second_lastname'].replace(@dni.second_lastname)
# doc.bookmarks['names'].replace(@dni.names)
# doc.bookmarks['dni_number'].replace(@dni.dni_number)
file_path = Rails.root.join('tmp', 'generated_doc.docx')
doc.save(file_path)
file_path
else
raise "El archivo de plantilla no se encuentra en la ruta: #{TEMPLATE_PATH}"
end
end
def generate_pdf
pdf = Prawn::Document.new
pdf.text "First Lastname: #{@dni.first_lastname}"
pdf.text "Second Lastname: #{@dni.second_lastname}"
pdf.text "Names: #{@dni.names}"
pdf.text "DNI Number: #{@dni.dni_number}"
file_path = Rails.root.join('tmp', 'generated_pdf.pdf')
pdf.render_file(file_path)
file_path
end
end
The problem is when I try to open the file to replece the bookmarks with my data:
10: def generate_docx
11: byebug
=> 12: if File.exist?(TEMPLATE_PATH)
13: doc = Docx::Document.open(TEMPLATE_PATH)
14: doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
15: puts bookmark_name
16: end
(byebug) Docx::Document.open(TEMPLATE_PATH)
*** NoMethodError Exception: undefined method `close' for nil
nil
I already try move the file, use Docx::Document.parse(File.read(TEMPLATE_PATH)), but nothing appear solve the issue.