Coder Social home page Coder Social logo

ilovepdf-ruby's Introduction

iLovePDF Api - Ruby Library

Build Status GitHub version License: MIT

A library in ruby for iLovePDF Api

You can sign up for a iLovePDF account at https://developer.ilovepdf.com

Develop and automate PDF processing tasks like:

  • Compress PDF
  • Merge PDF
  • Split PDF
  • Convert Office to PDF
  • PDF to JPG
  • Images to PDF
  • Add Page Numbers
  • Rotate PDF
  • Unlock PDF
  • Protect PDF
  • Stamp a Watermark
  • Repair PDF
  • PDF to PDF/A
  • Validate PDF/A
  • Extract
  • Sign PDF

Each one with several settings to get your desired results.

Requirements

Ruby 2.6 or greater

Installation

Add this line to your application's Gemfile:

gem 'ilovepdf'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ilovepdf

Usage

Getting started

The quickest way to get started is to first get a set of API keys here and run the following code snippet:

public_key  = 'YOUR_PUBLIC_KEY'
private_key = 'YOUR_PRIVATE_KEY'
ilovepdf = Ilovepdf::Ilovepdf.new(public_key, private_key)
# Create a task with the tool you want to use:
task = ilovepdf.new_task :compress
# Add the files you want to upload...
file1 = task.add_file 'my_disk/my_example1.pdf'
file2 = task.add_file 'my_disk/my_example2.pdf'
file3 = task.add_file_from_url 'http://URL_TO_PDF'
# Once you are done uploading your files:
task.execute
task.download

That's it!

For a more in-depth usage, refer to the sample codes in this repository.

Signature Tool

The usage of this tool is different than the other tools, the following example shows how to create a signature using the iLovePDF API:

my_task = Ilovepdf::Tool::Signature.new(pub_key, priv_key)
# File object keeps information about its server_filename and the properties you can set
file  = my_task.add_file '/path/to/file/sample.pdf'

signer = Ilovepdf::Signature::Receiver.new(:signer,'name','[email protected]')

signature_element = Ilovepdf::Signature::SignatureElement.new(file)
signature_element.set_position(x: 20, y: -20)
signature_element.pages = "1"
signature_element.size = 40

signer << signature_element
body = my_task.send_to_sign.body

For a more in-depth usage, refer to all of the signature examples on the sample codes in this repository.

Documentation

HTTP API Calls

All PDF tools have the following methods that contact with the iLovePDF API:

Method Description Notes
add_file(file) Uploads a file to iLovepdf servers Returns boolean
add_file_from_url(url) Uploads a file to iLovepdf servers using a URL Returns boolean
delete_file(file) Deletes a file from ilovepdf Returns boolean
download(filepath) Downloads the processed file Returns boolean; No need to specify a filepath
status Retrieves the current status of the task being processed Returns Ilovepdf::Response
execute Sends a request to Ilovepdf to begin processing the PDFs Returns Ilovepdf::Response
delete! Deletes the task Returns Ilovepdf::Response

Example:

imagepdf_task = Ilovepdf::Tool::Imagepdf.new(public_key, secret_key)
http_response = imagepdf_task.execute
puts http_response.body
if imagepdf_task.download
  puts "Your file was downloaded successfully!"
end

Methods common to all tools

Method Description Notes
enable_file_encryption(boolean, key) The key will be used to decrypt the files before processing and re-encrypt them after processing If no key provided, a random key is assigned (default: false)
assign_meta_value(key, value) More info
ignore_errors More info (default: true)
ignore_password More info (default: true)
try_pdf_repair When a PDF to process fails we try to repair it automatically (default: true)
packaged_filename This allows you to specify the filename of the compressed file in case there is more than 1 file to be downloaded
output_filename The final name of the processed file

Methods to query after performing the execute API method:

  • result: It has stored the last Ilovepdf::Response

Methods to query after performing the download API method:

  • download_info: Returns a struct with the following info
    • :output_filename
    • :output_file
    • :output_filetype

Tool attributes

All tools have specific attributes you can access and modify. If you need want to know which params are available for a specific tool via the code, you can by looking at the API_PARAMS of that particular tool. For example for the Image to PDF tool:

puts Ilovepdf::Tool::Imagepdf::API_PARAMS
# => [:orientation, :margin, :pagesize]

To instantiate a Compress tool task directly do:

compress_task = Ilovepdf::Tool::Compress.new(public_key, secret_key)

Handling errors

Whenever there is an API Error in one of the endpoints, you can try to capture it the following way:

begin
  compress_task = Ilovepdf::Tool::Compress.new(public_key, secret_key)
  compress_task.execute # Oops, this raises an error! I forgot to upload a file!
  compress_taks.download
rescue Ilovepdf::ApiError => e
  # Let's check what went wrong with the API call:
  puts e.http_response.body
end

For a more complete example of error handling check samples/try_catch_errors.rb

Please see https://developer.ilovepdf.com/docs for up-to-date documentation.

License

The gem is available as open source under the terms of the MIT License.

ilovepdf-ruby's People

Contributors

benjamin-ltr avatar guillem-pdf avatar lcollazo-ilovepdf avatar leoschronicles avatar marcogrossisas avatar maztch avatar rpbaltazar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ilovepdf-ruby's Issues

JWT version is stopping platform updates

JWT gem has been last updated in 2017 and is currently in version 2.1.
You're limiting the compatible version in your gem, which is stopping us from updating the whole platform, particularly the oauth system.

Any plans on upgrading the gem?

Ruby 3 support: URI.unescape

On ruby 3, we have some errors using the ilovepdf gem:

undefined method `unescape' for URI:Module

This method was deprecated on ruby 2.7 and removed on ruby 3.
Also, the README file specifies that this gem requires Ruby 2.6 or greater but the travis Ci is configured for ruby 2.5.0 only.
It can be good to specify multiple ruby versions for the CI runs.

Thanks

Releasing the gem

Hello,

The current published version (1.2.0) have a hard dependency on jwt 1.5.6

It is fixed on master so we can use jwt 2, could you release the new version?

Regards

New release; jwt version issue again...

Hi, would it be possible to get a release that depends on gem 'jwt', '>= 2.2' instead of gem 'jwt', '~> 2'? There's a (unnecessary, I think, and unfortunate either way) conflict with other Gems which need the JWT 2 major release to be minor release 2.2 or higher.

Fix bad setter name on pdfjpg class

After installing the latest version, I now have a new bug on the pdfjpg class:

 ArgumentError:
       wrong number of arguments (given 0, expected 1)

The problem comes from a badly name setter method introduced by 305e551 (not released with the previous version)

I also generated the code coverage for this gem and it's lacking unit tests:

Screenshot 2024-05-16 at 14 35 06

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.