如何在ruby中组合PDF?
这是在2008年被问到的 。 希望现在有更好的答案。
如何在ruby中组合PDF?
我正在使用pdf-stamper gem填写PDF格式的表格。 我想拍摄n个 PDF,在每个PDF中填写一个表单,并将结果保存为n页文档。
你能用像prawn这样的本地库来做到这一点吗? 你能用rjb和iText做到这一点吗? pdf-stamper是iText的包装器。
如果可能的话,我想避免使用两个库(即pdftk和iText)。
截至2013年,您可以使用Prawn合并pdf。 要点: https : //gist.github.com/4512859
class PdfMerger def merge(pdf_paths, destination) first_pdf_path = pdf_paths.delete_at(0) Prawn::Document.generate(destination, :template => first_pdf_path) do |pdf| pdf_paths.each do |pdf_path| pdf.go_to_page(pdf.page_count) template_page_count = count_pdf_pages(pdf_path) (1..template_page_count).each do |template_page_number| pdf.start_new_page(:template => pdf_path, :template_page => template_page_number) end end end end private def count_pdf_pages(pdf_file_path) pdf = Prawn::Document.new(:template => pdf_file_path) pdf.page_count end end
经过长时间搜索纯Ruby解决方案后,我最终从头开始编写代码来解析和合并/合并PDF文件。
(我觉得现在的工具太乱了 – 我想要一些原生的东西,但它们似乎都有不同的问题和依赖…甚至Prawn放弃了他们用来拥有的模板支持)
我在网上发布了gem,你也可以在GitHub上找到它。
你可以安装它:
gem install combine_pdf
它非常易于使用(有或没有将PDF数据保存到文件中)。
例如,这是一个“单行”:
(CombinePDF.load("file1.pdf") << CombinePDF.load("file2.pdf") << CombinePDF.load("file3.pdf")).save("out.pdf")
如果您发现任何问题,请告诉我,我会解决问题。
使用ghostscript组合PDF:
options = "-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite" system "gs #{options} -sOutputFile=result.pdf file1.pdf file2.pdf"
我写了一个rubygem来做到这一点 – PDF :: Merger 。 它使用iText。 这是你如何使用它:
pdf = PDF::Merger.new pdf.add_file "foo.pdf" pdf.add_file "bar.pdf" pdf.save_as "combined.pdf"
没有在Ruby中看到很好的选择 – 我得到了最好的结果炮轰到pdftk :
system "pdftk #{file_1} multistamp #{file_2} output #{file_combined}"
我们比2008年更接近,但还没到那里。
Prawn的最新开发版本允许您使用现有PDF作为模板,但在添加更多页面时不能反复使用模板。
通过iText,这将有效…虽然您应该在合并它们之前展平它们以避免字段名称冲突。 那个或一次重命名一个页面的字段。
在PDF中,具有相同名称的字段共享一个值 。 这通常不是理想的行为,尽管它不时会派上用场。
(在java中)的一些东西:
PdfCopy mergedPDF = new PdfCopy( new Document(), new FileOutputStream( outPath ); for (String path : paths ) { PdfReader reader = new PdfReader( path ); ByteArrayOutputStream curFormOut = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper( reader, curFormOut ); stamper.setField( name, value ); // ad nauseum stamper.setFlattening(true); // flattening setting only takes effect during close() stamper.close(); byte curFormBytes = curFormOut.toByteArray(); PdfReader combineMe = new PdfReader( curFormBytes ); int pages = combineMe .getNumberOfPages(); for (int i = 1; i <= pages; ++i) { // "1" is the first page mergedForms.addPage( mergedForms.getImportedPage( combineMe, i ); } } mergedForms.close();
如果您想使用combine_pdf gem添加任何模板(由macOS Pages或Google Docs创建),那么您可以尝试使用:
final_pdf = CombinePDF.new company_template = CombinePDF.load(template_file.pdf).pages[0] pdf = CombinePDF.load (content_file.pdf) pdf.pages.each {|page| final_pdf << (company_template << page)} final_pdf.save "final_document.pdf"