PDF to PDF Ruby Examples

These examples demonstrate common PDF to PDF API workflows in Ruby.


Basic examples

Multiple PDFs to PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/path/to/cover.pdf")
    client.addPdfFile("/path/to/proposal.pdf")
    client.addPdfFile("/path/to/price.pdf")
    client.addPdfFile("/path/to/contact.pdf")

    # Run the conversion and save the result to a file.
    client.convertToFile("offer.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Multiple PDFs to in-memory PDF

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/path/to/cover.pdf")
    client.addPdfFile("/path/to/proposal.pdf")
    client.addPdfFile("/path/to/price.pdf")
    client.addPdfFile("/path/to/contact.pdf")

    # Run the conversion and store the result in the `pdf` variable.
    pdf = client.convert()

    # at this point the "pdf" variable contains PDF raw data and
    # can be sent in an HTTP response, saved to a file, etc.

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Multiple PDFs to PDF stream

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/path/to/cover.pdf")
    client.addPdfFile("/path/to/proposal.pdf")
    client.addPdfFile("/path/to/price.pdf")
    client.addPdfFile("/path/to/contact.pdf")

    # Create an output stream for the conversion result
    output_stream = open("offer.pdf", "wb")

    # run the conversion and write the result to the output stream.
    client.convertToStream(output_stream)

    # Close the output stream.
    output_stream.close()

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Multiple in-memory PDFs to PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfRawData(open('/path/to/cover.pdf', 'rb').read())
    client.addPdfRawData(open('/path/to/proposal.pdf', 'rb').read())
    client.addPdfRawData(open('/path/to/price.pdf', 'rb').read())
    client.addPdfRawData(open('/path/to/contact.pdf', 'rb').read())

    # Run the conversion and save the result to a file.
    client.convertToFile("offer.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Join 2 in-memory PDFs together with 2 local PDF files to PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfRawData(open('/path/to/cover.pdf', 'rb').read())
    client.addPdfFile("/path/to/proposal.pdf")
    client.addPdfRawData(open('/path/to/price.pdf', 'rb').read())
    client.addPdfFile("/path/to/contact.pdf")

    # Run the conversion and save the result to a file.
    client.convertToFile("offer.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Watermark a PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/path/to/proposal.pdf")
    client.setPageWatermark("/path/to/watermark.pdf")

    # Run the conversion and save the result to a file.
    client.convertToFile("company_offer.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Linearize a PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/path/to/not_linearized.pdf")
    client.setLinearize(true)

    # Run the conversion and save the result to a file.
    client.convertToFile("linearized.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Get info about the current conversion

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.setDebugLog(true)
    client.addPdfRawData(open('/path/to/cover.pdf', 'rb').read())
    client.addPdfRawData(open('/path/to/proposal.pdf', 'rb').read())

    # Run the conversion and save the result to a file.
    client.convertToFile("offer.pdf")
    
    # print URL pointing to the debug log for this request.
    puts "Debug log url: #{client.getDebugLogUrl()}"
    
    # print number of conversion credits remaining in your account.
    puts "Remaining credit count: #{client.getRemainingCreditCount()}"
    
    # print number of credits consumed for this conversion.
    puts "Consumed credit count: #{client.getConsumedCreditCount()}"
    
    # print unique identifier assigned to this conversion job.
    puts "Job id: #{client.getJobId()}"
    
    # print total number of pages in the output document.
    puts "Page count: #{client.getPageCount()}"
    
    # print size of the output data in bytes.
    puts "Output size: #{client.getOutputSize()}"

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

PDF manipulation examples

Extract page 3 and all pages from 7 to the end from the PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/your-path-to/pdfs/13_pages.pdf")
    client.setAction("extract")
    client.setPageRange("3,7-")

    # Run the conversion and save the result to a file.
    client.convertToFile("output.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Delete the first 3 pages and the 10th page from the PDF file

require "pdfcrowd"

begin
    # Create an API client instance.
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

    # Configure the conversion.
    client.addPdfFile("/your-path-to/pdfs/13_pages.pdf")
    client.setAction("delete")
    client.setPageRange("1-3,10")

    # Run the conversion and save the result to a file.
    client.convertToFile("output.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error: #{why}"
    raise
end

Split the PDF file into two separate files at the 10th page

require "pdfcrowd"

begin
    client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")
    client.addPdfFile("/your-path-to/pdfs/13_pages.pdf")
    client.setAction("extract")
    client.setPageRange("1-10")
    client.convertToFile("pages1-10.pdf")
    client.setPageRange("11-")
    client.convertToFile("pages11-end.pdf")

rescue Pdfcrowd::Error => why
    STDERR.puts "PDFCrowd Error:
    raise
end

Rails examples

Multiple PDFs to PDF in Rails

require "pdfcrowd"

class DemoController < ApplicationController
    def convert
        # The recommended method is POST.
        # may be restricted by ":via => :post" in your routes.rb
        if ! request.post?
            return render text: "POST is allowed only", status: 400
        end

        begin
            # Create an API client instance.
            client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

            # Configure the conversion.
            client.addPdfFile("/path/to/cover.pdf")
            client.addPdfFile("/path/to/proposal.pdf")
            client.addPdfFile("/path/to/price.pdf")
            client.addPdfFile("/path/to/contact.pdf")

            # Run the conversion and store the result in the `pdf` variable.
            pdf = client.convert()

            # Send the result and set HTTP response headers.
            send_data pdf,
                      :type => "application/pdf",
                      :disposition => "attachment; filename*=UTF-8''#{ERB::Util.url_encode('offer.pdf')}"
        rescue Pdfcrowd::Error => why
            # Send the error in the HTTP response.
            render plain: why, status: why.getCode()
        end
    end
end

Multiple in-memory PDFs to PDF in Rails

require "pdfcrowd"

class DemoController < ApplicationController
    def convert
        # The recommended method is POST.
        # may be restricted by ":via => :post" in your routes.rb
        if ! request.post?
            return render text: "POST is allowed only", status: 400
        end

        begin
            # Create an API client instance.
            client = Pdfcrowd::PdfToPdfClient.new("demo", "demo")

            # Configure the conversion.
            client.addPdfRawData(open('/path/to/cover.pdf', 'rb').read())
            client.addPdfRawData(open('/path/to/proposal.pdf', 'rb').read())
            client.addPdfRawData(open('/path/to/price.pdf', 'rb').read())
            client.addPdfRawData(open('/path/to/contact.pdf', 'rb').read())

            # Run the conversion and store the result in the `pdf` variable.
            pdf = client.convert()

            # Send the result and set HTTP response headers.
            send_data pdf,
                      :type => "application/pdf",
                      :disposition => "attachment; filename*=UTF-8''#{ERB::Util.url_encode('offer.pdf')}"
        rescue Pdfcrowd::Error => why
            # Send the error in the HTTP response.
            render plain: why, status: why.getCode()
        end
    end
end