In this tutorial, we will learn how to generate Word or PDF documents using the Documentero API service service in Ruby. Documentero is an API service that allows you to generate documents based on templates and data provided.

Prerequisites

Before we begin, make sure you have the following:

  • Ruby installed on your machine
  • Documentero API access and your API secret key

Step 1: Setting up the Project

Let’s start by setting up a new Ruby project. Open your preferred text editor or IDE and follow these steps:

  1. Create a new project directory for your Ruby script.

  2. Open a terminal or command prompt and navigate to the project directory.

Step 2: Making a POST Request to Documentero API

Next, we’ll write the code to make a POST request to the Documentero API service and generate a document.

In your project directory, create a new Ruby file, e.g., documentero_demo.rb, and open it in your text editor or IDE.

Now, let’s define a method to generate the document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'net/http'
require 'json'

def generate_document
documentero_endpoint = URI('https://app.documentero.com/api')

api_key = 'YOUR_API_KEY' # Replace with your API secret key
template_identifier = 'Pt04DVNbifqY4inserI2' # Replace with your template identifier
format = 'docx' # Set the desired format: 'docx' for Word or 'pdf' for PDF
data = { 'field' => 'fieldvalue' } # Replace with the data you want to pass to the document generation

request_data = {
'document' => template_identifier,
'apiKey' => api_key,
'format' => format,
'data' => data
}

request = Net::HTTP::Post.new(documentero_endpoint)
request.body = request_data.to_json
request.content_type = 'application/json'

response = Net::HTTP.start(documentero_endpoint.hostname, documentero_endpoint.port, use_ssl: true) do |http|
http.request(request)
end

response_data = JSON.parse(response.body)

status = response_data['status']
message = response_data['message']
download_link = response_data['data']

if status == 200
puts 'Document Generated Successfully'
puts 'Download Link: ' + download_link
else
puts 'Document Generation Failed: ' + message
end
end

generate_document

Make sure to replace 'YOUR_API_KEY' with your actual API secret key obtained from Documentero, and 'Pt04DVNbifqY4inserI2' with the template identifier provided by Documentero.

Step 3: Running the Code

Save the changes to the documentero_demo.rb file. In your terminal or command prompt, navigate to the project directory.

To execute the code and generate the document, run the following command:

1
ruby documentero_demo.rb

If everything is set up correctly, you should see the following output:

1
2
Document Generated Successfully
Download Link: [ExpirableLinkToDownloadDocument]

The [ExpirableLinkToDownloadDocument] will be the actual link to download the generated document, which can be either a Word document (.docx) or a PDF file (.pdf).

Congratulations! You have successfully generated a document using the Documentero API service in Ruby.

Conclusion

In this tutorial, we learned how to generate Word or PDF documents in Ruby using the Documentero API service. We made a

POST request to the Documentero API and handled the response to retrieve the download link for the generated document.