How to use webPDF with Ruby via REST

This example shows how to use the webservices of webPDF with the scripting language Ruby via the REST interface. To follow this simple example on your PC you need a locally installed webPDF, Ruby version 2.x and any text editor. If you need help installing Ruby, please refer to ruby-lang.org/en/documentation/installation.

Set up the project

First, check that the Ruby setup was successful by typing the commands

‘gem’ and ‘ruby –v’

one after the other. If both commands are known, the installation was successful.

Then use the command prompt to install the required ‘gem ‘rest-client’ with the command ‘gem install ‘rest-client’. Gems are libraries that can be managed using the standard Ruby application. Now you are ready to start writing the example program. In the directory of your choice, use the text editor of your choice to create a file with the extension ‘.rb’ for your code. You can also create a file with the name ‘test.txt’ for any entries you like. Your program will later convert this file to a PDF document.

Writing the code

require 'rest-client'
require 'json'

With this information you can include the required gems in your script. json’ is used to parse and generate JSON objects and is included in the Ruby standard library. rest-client’ allows you to easily work through the REST interface and was previously installed by you using the ‘gem’ command.

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

First, the base URL of your webPDF application is stored in a constant. The URL to the authentication-service of webPDF is formed from this constant and the attached string.

There a token for your session is created with the parameters ‘username’ and ‘password’. This is extracted from the returned JSON-object into the variable ‘token’.

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

Here the file ‘test.txt’ is loaded onto the server via the Documents-service of webPDF. The previously generated token is placed in the header with the identifier ‘Token’. The document ID is extracted from the response JSON-object.

url=WEBPDF_URL+"/rest/converter/#{txt_document_id}/convert"
conversion_options={serviceOptions: '',
                    sourceExtension: '.txt',
                    targetExtension: '.pdf',
                    converterOptions: 'pdf.compression=true&pdf.jpegQuality=90'}
resp=RestClient.post url, JSON.generate(conversion_options), {content_type: :json,
                                                              accept: :json,
                                                              token: token}
pdf_document_id=JSON.parse(resp)['documentId']

In this section, the text file is converted into a PDF document. The URL to the converter service is formed with the document ID of the text file and in the body of the post there is a JSON object that contains the conversion options (this is previously generated from a Ruby hash). Details about the parameters can be found in the webPDF documentation. An overview of the REST methods can be found here. The document ID of the generated PDF file is extracted from the response.

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

With the document ID of the PDF file in the URL, the Documents service is addressed and the file is queried. This is then written to the file ‘test_result.pdf’.

Now execute the program by navigating to the project folder with the command prompt and start the script with the command ‘ruby fileName.rb’. Then a PDF file with the name ‘test_result.pdf’ and the content of the file ‘test.txt’ should be in your project folder.

As you can see, webPDF with Ruby, like most other scripting languages, is very easy to use via the REST interface.

Attachment:

All the source code:

require 'rest-client'
require 'json'

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

url=WEBPDF_URL+'/rest/authentication/user/login'
resp=RestClient.get(url,
                    {params: {username: 'admin', password: 'admin'}})
token=JSON.parse(resp)['token']

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

url=WEBPDF_URL+"/rest/converter/#{txt_document_id}/convert"
conversion_options={serviceOptions: '',
                    sourceExtension: '.txt',
                    targetExtension: '.pdf',
                    converterOptions: 'pdf.compression=true&pdf.jpegQuality=90'}
resp=RestClient.post url, JSON.generate(conversion_options), {content_type: :json,
                                                              accept: :json,
                                                              token: token}
pdf_document_id=JSON.parse(resp)['documentId']

url=WEBPDF_URL+"/rest/documents/#{pdf_document_id}"
resp=RestClient.get url, {token: token}

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