How to use webPDF with Ruby via REST

Lightbulb image: guide and tutorial

This example shows how to use webPDF webservices with the Ruby scripting language via the REST interface. To follow the steps on your machine, you need a local webPDF installation, Ruby version 2.x, and any text editor. If you need help installing Ruby, see ruby-lang.org/en/documentation/installation.

Set up the project

First, verify that Ruby is installed correctly by running these commands in your terminal:

  • gem
  • ruby --v

If both commands are recognized, your installation is working.

Then install the required gem rest-client:

gem install rest-client

Gems are Ruby libraries managed with built-in Ruby tooling. Next, create a .rb file for your code in any folder. Also create a file named test.txt and add any sample content. This file will be converted to PDF later.

Write the code

require 'rest-client'
require 'json'

These imports load the required gems. json is part of Ruby's standard library and is used to parse and generate JSON objects. rest-client enables convenient REST calls.

WEBPDF_URL='http://localhost:8080/webPDF'
url=WEBPDF_URL+'/rest/authentication/user/login'
resp=RestClient.get(url,
})
token=JSON.parse(resp)['token']

First, the base URL of your webPDF instance is stored in a constant. Then the authentication URL is built from this base path.

A session token is created there using username and password. The token is read from the returned JSON object and stored in the token variable.

url=WEBPDF_URL+'/rest/documents'
file=File.new('test.txt', 'rb')
resp=RestClient.post url, ,
txt_document_id=JSON.parse(resp)['documentId']

In this step, test.txt is uploaded to the webPDF server through the Documents service. The previously generated token is passed in the request header as Token. The response contains the documentId, which is extracted.

url=WEBPDF_URL+"/rest/converter/#/convert"
conversion_options=
resp=RestClient.post url, JSON.generate(conversion_options),
pdf_document_id=JSON.parse(resp)['documentId']

Now the text file is converted to a PDF document. The converter URL is built with the source document ID. The POST body contains conversion options as JSON (typically generated from a Ruby hash). Parameter details are available in the webPDF documentation. You can also inspect available REST methods at localhost:8080/webPDF/help/restful/. The returned documentId points to the generated PDF.

url=WEBPDF_URL+"/rest/documents/#"
resp=RestClient.get url,
File.open('test_result.pdf', 'wb') do |file|
file << resp
end

Using the PDF document ID in the URL, the Documents service is called to fetch the generated file. The response is written to test_result.pdf.

Run the program by opening your project folder in the terminal and starting the script with:

ruby fileName.rb

You should then find a test_result.pdf file in your project folder containing the converted content of test.txt.

As you can see, webPDF can be used with Ruby very easily through the REST interface.

Attachment

Complete source code:

require 'rest-client'
require 'json'

WEBPDF_URL='http://localhost:8080/webPDF'

url=WEBPDF_URL+'/rest/authentication/user/login'
resp=RestClient.get(url,
})
token=JSON.parse(resp)['token']

url=WEBPDF_URL+'/rest/documents'
file=File.new('test.txt', 'rb')
resp=RestClient.post url, ,
txt_document_id=JSON.parse(resp)['documentId']

url=WEBPDF_URL+"/rest/converter/#/convert"
conversion_options=
resp=RestClient.post url, JSON.generate(conversion_options),
pdf_document_id=JSON.parse(resp)['documentId']

url=WEBPDF_URL+"/rest/documents/#"
resp=RestClient.get url,

File.open('test_result.pdf', 'wb') do |file|
file << resp
end