How to Post a PDF to an API Endpoint

If you require to generate a PDF document and send it to an API endpoint, please follow the instructions below:

Illustrated below is an overview of the process, We generate a PDF document and send it to a REST API, The endpoint will trigger a flow to take the file and send it to an SFTP server.

image

Send PDF to SFTP Server

Illustrated in the image below is an example of a flow that will be used by the REST API to send the PDF generated to an SFTP server

Create a flow parameter to pass in the name of the file

Pass through step is used as a reader to get the file into the flow.

Byte group step is used to Group an incoming stream across several chunks into a single byte, Specify Input as Byte and output as Byte

image

Add a Combined step that will take the records and combine them into a single output to send to the SFTP server

The SFTP Server step will send the output to the Server, Specify the path on the server and pass in the file name as a parameter

A RunFile step can be added to download the file on a completed run, Specify the Input as Record and choose File Type

REST Gateway to Post the PDF to a destination

Create a REST Gateway that will use query parameters for the File Name and hit the flow created in the previous step

Choose the HTTP method as POST and provide a name to the path.
Under the Definitions section, Provide a Summary and Description if needed and add a Security Scheme to secure the endpoint with a key
In this case, we’ll be using Query Parameters to pass in the name of the file, In the Parameters table, Add the name of the parameter, and mark it as required as highlighted below.

Under the Source section, Choose the source as Flow, Select the flow created in the previous step.

To Parameter will be automatically added after selecting the name of the flow
In the From Path, map the parameters created in the Definition section as reqQuery.NameOfFile

Select the processing mode as Aync and the Input Content Type as None

Generate PDF

Illustrated in the image below is an example of a flow that gets data from a Buffer, groups the data and Generates a PDF document that will be posted to the REST endpoint.

Add a buffer containing data as a source step, Specify the config, and Save the step.

Add a Group step that will group the records by a specific set of keys and sends the results through as an items array. A default calculator can be added after the group step to see the results.

image

Below is a sample of what the data looks like before the Combined step

After the Group and Calculator, Add a Combined step that will take the records and combines them into a single output for the PDF writer step

image

In the PDF writer step, Build out the record template. In this example we’ll create a simple HTML table to display the list of Food items with their details.
We’ll loop over the items array as highlighted in yellow and include the details for each item as highlighted in green

Note: The code icon must be closed before you save or changes will not be applied, Click on the icon as circled in red above.

The table and details should be shown as per the image below.

Scroll to the bottom of the PDF writer step, In the callback URL we can specify the endpoint details and pass in any query parameters

https://rest.synatic.com/org/sendpdf/pdf?apiKey=KeyGoesHere&NameOfFile=testfile.pdf
NameOfFile can be passed in dynamically as: NameOfFile={@record.FieldName} or NameOfFile={@parameters.FieldName}

Notes on the PDF Callback:

  • The file is sent as a stream with content type of: application/octet-stream
  • Supports query string parameters, but the parameters must be set in the actual URL. For example if the URL field would be https://some-url.com?someParam=someValue
  • An HTTP destination can also be used to achieve the same result, In terms of Speed and Resources, There would be better performance out of an HTTP destination step which will run on the worker rather than the service that does the PDF conversion.

Execute the Generate PDF flow which will create the file, Hit the REST API, triggering the Send PDF to SFTP server, and move the file over to the SFTP server. Below is a sample of the file generated and sent to the SFTP server

image