Image to PDF Ruby Examples
Complete Ruby examples for common Image to PDF tasks. See the Ruby SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Ruby example can be copied and run independently.
Basic examples
Choose file, in-memory, or output-stream handling for the PDF result. Stream examples use a binary file stream as their destination.
Convert an image file
Upload an existing logo.png file.
Save to a file
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.convertFileToFile('logo.png', 'logo.pdf')
Keep in memory
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') pdf = client.convertFile('logo.png') # binary String puts pdf.bytesize
Write to a stream
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') File.open('logo.pdf', 'wb') do |output_stream| client.convertFileToStream('logo.png', output_stream) end
Convert a URL
Fetch an image from its URL.
Save to a file
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.convertUrlToFile('https://your-server.com/logo.png', 'logo.pdf')
Keep in memory
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') pdf = client.convertUrl('https://your-server.com/logo.png') # binary String puts pdf.bytesize
Write to a stream
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') File.open('logo.pdf', 'wb') do |output_stream| client.convertUrlToStream('https://your-server.com/logo.png', output_stream) end
Convert raw image bytes
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
Save to a file
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') image_data_1 = File.binread('logo.png') # binary String client.convertRawDataToFile(image_data_1, 'logo.pdf')
Keep in memory
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') image_data_1 = File.binread('logo.png') # binary String pdf = client.convertRawData(image_data_1) # binary String puts pdf.bytesize
Write to a stream
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') File.open('logo.pdf', 'wb') do |output_stream| image_data_1 = File.binread('logo.png') # binary String client.convertRawDataToStream(image_data_1, output_stream) end
Read from an input stream
Open logo.png as a binary input stream and save the result to logo.pdf.
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') File.open('logo.png', 'rb') do |input_stream| client.convertStreamToFile(input_stream, 'logo.pdf') end
Customize the PDF
These examples use scan.png, a 1224 × 1584 pixel image with text, a chart, and a uniform white border. The rotation example uses its sideways variant, scan-sideways.png.
Fit an image onto paper
Fit the whole image onto A4 portrait or Letter landscape paper with 12mm margins and 150 DPI. The fit mode preserves the image's aspect ratio. Setting a page size without it can clip a larger image.
A4 portrait
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(150) client.setMarginBottom('12mm') client.setMarginLeft('12mm') client.setMarginRight('12mm') client.setMarginTop('12mm') client.setPageSize('A4') client.setPrintPageMode('fit') client.convertFileToFile('scan.png', 'a4.pdf')
Letter landscape
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(150) client.setMarginBottom('12mm') client.setMarginLeft('12mm') client.setMarginRight('12mm') client.setMarginTop('12mm') client.setOrientation('landscape') client.setPageSize('Letter') client.setPrintPageMode('fit') client.convertFileToFile('scan.png', 'landscape.pdf')
Create a custom-size PDF
Fit scan.png onto a 4 × 6 inch page and save custom-page.pdf. Preserve the aspect ratio and fill the unused page area with a pale blue background.
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(150) client.setPageBackgroundColor('EEF2F6') client.setPageHeight('6in') client.setPageWidth('4in') client.setPrintPageMode('fit') client.convertFileToFile('scan.png', 'custom-page.pdf')
Set the physical print size
Interpret scan.png at 300 pixels per inch without setting a fixed page size. Its 1224 × 1584 pixels produce a 4.08 × 5.28 inch page in print-size.pdf, with the original image pixels retained.
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(300) client.convertFileToFile('scan.png', 'print-size.pdf')
Crop or trim an image
Extract the chart from scan.png into chart.pdf, or remove its uniform outer border and save trimmed.pdf.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match the source image pixels; adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
Extract a selected region
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setCropAreaHeight('384px') client.setCropAreaWidth('1024px') client.setCropAreaX('96px') client.setCropAreaY('370px') client.setDpi(96) client.convertFileToFile('scan.png', 'chart.pdf')
Remove a uniform border
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(150) client.setRemoveBorders(true) client.convertFileToFile('scan.png', 'trimmed.pdf')
Correct image rotation
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.pdf.
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(150) client.setRotate('90') client.convertFileToFile('scan-sideways.png', 'upright.pdf')
Create a smaller PDF
Render scan.png onto A4 paper at 72 DPI and save compact.pdf. Keep the same fit and 12mm margins as the 150 DPI A4 example above, while reducing resolution for screen viewing. Smaller files come at the cost of fine detail.
require 'pdfcrowd' client = Pdfcrowd::ImageToPdfClient.new('demo', 'demo') client.setDpi(72) client.setMarginBottom('12mm') client.setMarginLeft('12mm') client.setMarginRight('12mm') client.setMarginTop('12mm') client.setPageSize('A4') client.setPrintPageMode('fit') client.convertFileToFile('scan.png', 'compact.pdf')