require 'hexapdf' require 'open-uri' module AlegoPdf class Merging attr_reader :filepaths, :filename def initialize(filepaths, filename) @filepaths = filepaths @filename = filename end def call target = HexaPDF::Document.new filepaths.each do |filepath| uri = URI.parse(filepath).open(&:read) tempfile = Tempfile.new tempfile.write(uri.force_encoding('UTF-8')) tempfile.close pdf = HexaPDF::Document.open(tempfile) pdf.pages.each { |page| target.pages << target.import(page) } end target.write(filename, optimize: true) end end end