Ruby: Reading PDF files

前端 未结 6 649
孤独总比滥情好
孤独总比滥情好 2020-12-02 06:05

I\'m looking for a fast and reliable way to read/parse large PDF files in Ruby (on Linux and OSX).

Until now I\'ve found the rather old and simple PDF-toolkit (a pd

6条回答
  •  一整个雨季
    2020-12-02 06:46

    Did you have a look at the CombinePDF library?

    It's a pure ruby solution that allows some PDF manipulation, such as extracting pages, overlaying one PDF page over another, page numbering, writing basic text and tables, etc'.

    Here's an example for stumping an existing PDF file with a logo. The example reads a PDF file, extracts one page to use as a stamp and stamps another PDF file.

    require 'combine_pdf'
    company_logo = CombinePDF.load("company_logo.pdf").pages[0]
    pdf = CombinePDF.load "content_file.pdf"
    pdf.pages.each {|page| page << company_logo}
    pdf.save "content_with_logo.pdf"
    

    You can also stamp text, number pages or use :

    require 'combine_pdf'
    
    pdf = CombinePDF.load "content_file.pdf"
    
    pdf.number_pages #adds page numbers. you can add formatting and placement options.
    
    pdf.pages.each {|page| page.textbox "One Way To Stamp"}
    
    #you can a shortcut method to stamp pages
    pdf.stamp_pages "Another way to stamp"
    
    #you can use the shortcut method for both text and PDF stamps
    company_logo = CombinePDF.load("company_logo.pdf").pages[0]
    pdf.stamp_pages company_logo
    
    # you can use write simple tables
    pdf.pages[0].write_table headers: ['first name', 'surname'], table_data: [['John', 'Doe'], ['Mr.', 'Smith']]
    
    pdf.save "content_with_logo.pdf"
    

    It's not meant for complex operations, but it complements most PDF authoring libraries and allows you to use PDF templates instead of writing the whole thing from scratch.

提交回复
热议问题