Files
baclight/app/services/id_card_printer/pdf_processor.rb
T
2026-03-16 12:09:45 -04:00

40 lines
1.5 KiB
Ruby

module IdCardPrinter
class PdfProcessor
def initialize(pl_plan_key, layout, zip)
@pl_plan_key = pl_plan_key
@employer_card_config = Employer.find_by(pl_plan_key: pl_plan_key).id_card_configuration
@layout = layout
@zip = zip
end
def call
group_cards_pdf_array = []
IdCard::PrintData.where(pl_plan_key: @pl_plan_key).each do |card|
url = IdCardPrinter::JasperUrlGenerator.new(@pl_plan_key, card.family_id, @layout).call
puts url
card_pdf = IdCardPrinter::JasperPdfGenerator.new(url).call
if @zip
card_filename = "#{card.name.gsub(", ", "_")}_digital_card_#{Date.today}.pdf"
group_cards_pdf_array << { name: card_filename, data: card_pdf.to_pdf }
else
group_cards_pdf_array << card_pdf
end
end
if @zip
group_cards_pdf = Zip::OutputStream.write_buffer do |zio|
group_cards_pdf_array.each do |file|
zio.put_next_entry(file[:name])
zio.write(file[:data])
end
end
else
todays_date = DateTime.current.strftime('%Y%m%d%H%M%S')
group_cards_pdf.save("tmp/#{@employer.name}_print_cards_#{todays_date}.pdf")
end
group_cards_pdf
end
end
end