This documention is for Gem version 2.x.x, for documentation of Version 1.x.x, please refer the following README
Pdf4me is a PDF utility tool for Ruby and other languages.
The tool provides utility over common PDF related task like merge, create, stamp and others, which can be
found below.
Add this line to your application's Gemfile:
gem 'pdf4me'And then execute:
$ bundle
Or install it yourself as:
$ gem install pdf4me
Pdf4me works with any Rack application or plain old ruby script. In any regular script the configuration of Pdf4me looks like
- Require pdf4me
- Configure pdf4me with API endpoints and credentials
- Call appropriate methods.
require 'pdf4me'
Pdf4me.configure do |config|
config.host = 'api-dev.pdf4me.com'
config.token = 'valid-token'
endAll basic methods exposes a safe and a dangerous method to execute the operation. If the run fails, you can get the error through
.errors
Given a file, convert the file to PDF. if
save_pathis not provided, the converted PDF is by defauled saved as file_name provided.
b = Pdf4me::ConvertFileToPdf.new(
file: '/path/to/file/3.pdf',
save_path: 'converted.pdf'
)
b.run! # dangerous method
b.run # safe method - returns true|false
b.errors # to show errorsExtract particular set of pages from given PDF
a = Pdf4me::ExtractPages.new(
file: '/path/to/file/3.pdf',
pages: [4],
save_path: 'extracted.pdf'
)
a.runCreate Thumbnail from existing PDF for a particular page number. This takes in some additional configuration
widthdefaulting to 100px,image_formatdefaulting to PNG
a = Pdf4me::CreateThumbnail.new(
file: '/path/to/file/3.pdf',
page_number: 4,
width: 120,
save_path: 'thumbnail.png'
)
a.runMerges two PDFs, the order of merge is defined by the arguments passed.
file1would always be the first followed byfile2in merged PDF
a = Pdf4me::MergeTwoPdfs.new(
file1: '/path/to/file/3.pdf',
file2: '/path/to/file/4.pdf',
save_path: 'merged.pdf'
)
a.run###optimize_by_profile
Optimizes a PDF based on th the profile argument passed.
- valid values for profile are
default,web,max,mRC
a = Pdf4me::OptimizeByProfile.new(
file: '/path/to/file/3.pdf',
profile: 'max',
save_path: 'optimized.pdf'
)
a.runCreates a PDF/A Compliant document from a given PDF. By default it does not return any response.
- valid values for compliances are
unknown,pdf10,pdf11,pdf12,pdf13,pdf14,pdfA1b,pdfA1a,pdf15,pdf16,pdf17,pdfA2b,pdfA2u,pdfA2a,pdfA3b,pdfA3u,pdfA3a
a = Pdf4me::CreatePdfA.new(
file: '/path/to/file/4.pdf',
compliance: 'pdfA1a',
save_path: 'to_pdfa.pdf'
)
a.runSplit a given PDF into two with page number as the pivot point.
a = Pdf4me::SplitByPageNr.new(
file: '/path/to/file/3.pdf',
page_number: 3,
first_pdf: 'first.pdf', # defaults to '/path/to/file/3-first.pdf'
second_pdf: 'second.pdf' # defaults to '/path/to/file/3-second.pdf'
)
a.runStamp a PDF with a text on particular sets of pages Stamp takes additional arguments
position_x- Position at X-Axis, valid values areleft,center,rightposition_y- Position at Y-Axis, valid values aretop,middle,bottom- text - Text to stamp the PDF with and its mandatory.
a = Pdf4me::TextStamp.new(
file: '/path/to/file/3.pdf',
pages: [1, 3],
position_x: 'left',
position_y: 'top',
text: 'Hell Pdf4me',
save_path: 'stamped.pdf'
)
a.runThese methods are more complex than the Basic Wrapper methods.
By default all the Advanced methods run in blocking mode - i.e you wait for the server to send you the response.
If you want to run the operation asynchronously - you will be notified by a webhook over the result - then please configure the notification at the dashboard
See Pdf4me-Developer page to configure the webhook on application level.
###convert_to_pdf
Given a file, convert the file to PDF, with finer controls as opposed to
convert_file_to_pdf. The request is instantiated as a hash with the:reqkey, that takes inPdf4me::ConvertToPdfmodel.
The action takes two different models
:document,:convert_to_pdf_actionand returnsPdf4me::ConvertToPdfRes
To get more information regarding the models, please refer the provided model definition
file_path = '/path/to/file/TestDocToConvert.docx'
action = Pdf4me::ConvertToPdf.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read)),
name: "TestDocToConvert.docx"
),
convert_to_pdf_action: Pdf4me::ConvertToPdfAction.new
)
response = action.run
# saving converted file
File.open('path/to/save.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
end###optimize
Given a PDF document, Optimize the document and return
Pdf4me::OptimizeRes
:optimizetakes two different modelsdocumentandoptimize_action
Pdf4me::OptimizeActiontakes multiple arguments, important amongst them beingprofile
- profile, valid values are
default,web,max,mRC
The finer details on configuration can be found at the model definition
file_path = '/path/to/file/4.pdf'
action = Pdf4me::Optimize.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read))
),
optimize_action: Pdf4me::OptimizeAction.new(
use_profile: true,
profile: 'max'
)
)
response = action.run
# saving optimized file
File.open('path/to/optimized.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
endExtract out pages from PDF. You can choose from the pages you want to get.
extracttakes two model arguments
extract_actiondocument
To list all possible options for the models please refer the model definition
file_path = '/path/to/file/4.pdf'
action = Pdf4me::Extract.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read))
),
extract_action: Pdf4me::ExtractAction.new(
extract_pages: [1, 2, 5]
)
)
response = action.run
# saving extracted pages
File.open('path/to/extracted.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
endCreates images from the PDF Document. This is useful if you want to create thumbnails for the PDF.
The options are highly configurable to get the desired output. The output would be a base64 encoded object that you have to save.create_imagestakes two different arguments.
documentimage_action
Each model has their own set of attributes and hence can be configured further. The full documentation of model attributes are available at model definition
file_path = '/path/to/file/4.pdf'
action = Pdf4me::CreateImages.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read))
),
image_action: Pdf4me::ImageAction.new(
page_selection: Pdf4me::PageSelection.new(
page_nrs: [1]
),
image_extension: 'png',
center: true,
fit_page: true,
bits_per_pixel: 24,
bilevel_threshold: 181,
render_options: %w(noAntialiasing),
rotate_mode: 'none',
preserve_aspect_ratio: true,
compression: 'raw'
)
)
response = action.run
# saving the extracted thumbnail
File.open('thumbnail.png', 'wb') do |f|
f.write(Base64.decode64(response.document.pages.first.thumbnail))
endMerges two or more PDF and gives MergeRes, a Base64 Encoded file
mergetakes two model arguments
documents- An Array ofPdf4me::Documentmerge_action-Pdf4me::MergeActionmodel - non mandatory
To list all possible options for the models please refer the model definition
file_path_1 = '/path/to/file/4.pdf'
file_path_2 = '/path/to/file/3.pdf'
file_path_3 = '/path/to/file/2.pdf'
action = Pdf4me::Merge.new(
documents: [
Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path_1, 'rb', &:read))
),
Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path_2, 'rb', &:read))
),
Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path_3, 'rb', &:read))
)
]
)
response = action.run
# saving the merged PDF
File.open('merged.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
endCreates PDF/A Documents from normal PDF document.
pdf_atakes two model arguments.
document- a valid instance ofPdf4me::Documentpdf_a_action- a valid instance ofPdf4me::PdfAActionTo list all possible options for the models please refer the model definition
file_path = '/path/to/file/4.pdf'
action = Pdf4me::PdfA.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read))
),
pdf_a_action: Pdf4me::PdfAAction.new(
compliance: 'pdfA1a',
allowDowngrade: true,
allowUpgrade: true,
outputIntentProfile: 'sRGBColorSpace',
linearize: true
)
)
response = action.run
# saving the PDF/A document
File.open('pdf_a.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
endGiven a PDF, split the PDF into parts
splittakes up two models
document- a valid instance ofPdf4me::Documentsplit_action- a valid instance ofPdf4me::SplitActionwhich defines the number of parts the PDF splits intoTo list all possible options for the models please refer the model definition
file_path = '/path/to/file/4.pdf'
action = Pdf4me::Split.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read)),
),
split_action: Pdf4me::SplitAction.new(
split_after_page: 3
)
)
response = action.run
# save documents
response.documents.each_with_index do |document, index|
File.open("split_#{index}.pdf", 'wb') do |f|
f.write(Base64.decode64(document.doc_data))
end
endStamp a PDF, based on Stamp Action. You can stamp either a text or image to PDF, There are two separate methods defined to help you with stamping
- Stamp PDF with image
- Stamp PDF with Text
stamptakes two model arguments
document- a valid instance ofPdf4me::Document
stamp_action- a valid instance ofPdf4me::StampActionwhich defines the stamp type, and position.To list all possible options for the models please refer the model definition
file_path = '/path/to/file/4.pdf'
action = Pdf4me::Stamp.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read)),
),
stamp_action: Pdf4me::StampAction.new(
image: Pdf4me::Image.new(
image_data: Base64.encode64(File.open(image_path, 'rb', &:read)),
recetangle: Pdf4me::Rectangle.new(width: 200, height: 200)
),
)
)
response = action.run
# save stamped document
File.open('pdf_stamped.pdf', 'wb') do |f|
f.write(Base64.decode64(response.document.doc_data))
endBug reports and pull requests are welcome on GitHub at https://github.com/pdf4me/pdf4me-clientapi-ruby.
The gem is available as open source under the terms of the MIT License.