optimize merge pdf
This commit is contained in:
+11
-11
@@ -24,19 +24,19 @@ module PdfUtils
|
|||||||
decrypted_io.tap(&:rewind).read
|
decrypted_io.tap(&:rewind).read
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge(files)
|
def merge(io_files)
|
||||||
merged_pdf = HexaPDF::Document.new
|
merged_content = StringIO.new
|
||||||
|
|
||||||
files.each do |file|
|
Pdfium.with_instance do
|
||||||
pdf = HexaPDF::Document.new(io: file)
|
Pdfium::Document.create do |merged_pdf|
|
||||||
pdf.pages.each { |page| merged_pdf.pages << merged_pdf.import(page) }
|
io_files.each do |io|
|
||||||
|
Pdfium::Document.open_io(io) { |pdf| merged_pdf.import_pages(pdf) }
|
||||||
|
end
|
||||||
|
|
||||||
|
merged_pdf.save(merged_content)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
merged_content = StringIO.new
|
merged_content.tap(&:rewind)
|
||||||
merged_pdf.validate(auto_correct: true)
|
|
||||||
merged_pdf.write(merged_content, validate: false)
|
|
||||||
merged_content.rewind
|
|
||||||
|
|
||||||
merged_content
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user