This site uses cookies for better user experience. To use HTML PDF API, you must agree to our Privacy policy, including Cookie policy.
You can use our Ruby's gem hpa-ruby to send data to HTML PDF API service, but you can also use any gem from rubygems.org, that allows you to send an HTTP POST request. In this post we'll use rest-client Ruby's gem to send data to HTML PDF API.
The rest-client gem is a simple HTTP and REST client for Ruby, inspired by the Sinatra microframework, that provides a variety of methods to simplify the sending and retrieval of data via HTTP protocol.
To use rest-client you need to install MRI Ruby 1.9.3 or newer. MRI stands for Matz's Ruby Interpreter which is the reference implementation of the Ruby programming language named after Ruby creator Yukihiro Matsumoto, a.k.a. Matz.
There are a bunch of resources online how to install Ruby. We recommend you to visit following links for Ubuntu, Mac OSX and Windows.
Use the following command to check if the Ruby is installed:
ruby -v
Output should be similar:
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
or
ruby 2.2.3p173 (2015-08-18 revision 51636) [x64-mingw32]
After you have installed Ruby you are ready to install the rest-client gem. Type the following command in your terminal:
gem install rest-client
Use the following command to check if the rest-client is installed:
gem list | grep rest-client
Output should be similar:
rest-client (1.8.0)
Now you have installed all that we need, let's find out how to make calls to HTML PDF API service and generate PDF files.
You send data to HTML PDF API via HTML POST request. To generate PDF, you can use one of the following methods:
First you have to define parameters for the request. You can find the list of available parameters, as well as their description on HTML PDF API documentation page.
You can download all files used in the examples as a ZIP file. Don't forget to change the authentication token!!!
If you want to generate PDF file directly from URL, you must provide the 'url' parameter. In that case, 'html' and 'file' parameters must not be set. In this example, you'll send data by URL from remote HTML file and save the results in PDF file.
require 'rest-client'
api_url = 'https://htmlpdfapi.com/api/v1/pdf'
url = 'https://htmlpdfapi.com/examples/example.html'
# Don't forget to change the token!!!
response = RestClient.post(api_url, { url: url }, { Authentication: 'Token <your token>' })
File.open('result.pdf', 'w') do |file|
file.write(response)
end
Save previous code to url.rb file and run script from terminal:
ruby url.rb
You should see generated PDF file in the current directory.
The static RestClient.post
method receives three arguments:
Then we save HTTP response to PDF file. File.open
accepts a block and ensures that the File object will automatically be closed when the block terminates.
If you want to generate PDF from file, you must provide the 'file' parameter. In that case 'html' and 'url' parameters must not be set. This way you'll send HTML file and save result in PDF file.
require 'rest-client'
api_url = 'https://htmlpdfapi.com/api/v1/pdf'
file = File.new('example.html', 'r')
# Don't forget to change the token!!!
response = RestClient.post(api_url, { file: file }, { Authentication: 'Token <your token>' })
File.open('result.pdf', 'w') do |file|
file.write(response)
end
Save previous code to file.rb and run it from the terminal. You will need a example.html file in the current directory.
If you want to generate PDF file from HTML string, you must provide the 'html' parameter. In that case 'file' and 'url' parameters must not be set. In this example you'll send HTML string and save result in PDF file.
require 'rest-client'
api_url = 'https://htmlpdfapi.com/api/v1/pdf'
html = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>Hello world!!!</body></html>'
# Don't forget to change the token!!!
response = RestClient.post(api_url, { html: html }, { Authentication: 'Token <your token>' })
File.open('result.pdf', 'w') do |file|
file.write(response)
end
Save previous code to html.rb file and run it from the terminal.
If you want to generate PDF file from ZIP file, you must provide the 'file' parameter. In that case 'html' and 'url' parameters must not be set.
require 'rest-client'
api_url = 'https://htmlpdfapi.com/api/v1/pdf'
file = File.new('example.zip', 'rb')
# Don't forget to change the token!!!
response = RestClient.post(api_url, { file: file }, { Authentication: 'Token <your token>' })
File.open('result.pdf', 'w') do |file|
file.write(response)
end
Save previous code to zip.rb file and run it from terminal. You will need a example.zip file in the current directory.
In all examples you can send additional parameters as a second arguments of the post
method.
require 'rest-client'
api_url = 'https://htmlpdfapi.com/api/v1/pdf'
url = 'https://htmlpdfapi.com/examples/example.html'
params = {
url: url,
margin_top: '20',
page_size: 'A5',
orientation: 'landscape'
}
# Don't forget to change the token!!!
response = RestClient.post(api_url, params, { Authentication: 'Token <your token>' }) # Don't forget to change token!!!
File.open('result.pdf', 'w') do |file|
file.write(response)
end
In this example we send extra parameters margin_top
and page_size
. You can find the list of all available parameters on HTML PDF API documentation page.
Simple, isn't it?
Please feel free to leave your comments below.