Skip to main content

Convert Document to PDF

This guide walks you through converting a file to PDF format using the Stewart Sign API. This utility endpoint accepts a file upload and returns a PDF version of the document.

When to Use

Converting documents to PDF is useful when you need to:

  • Convert non-PDF documents (e.g., Word, images) to PDF before uploading them for eSignature
  • Ensure all documents in a transaction are in a consistent PDF format
  • Pre-process documents before creating eSign sessions

Requirements

  1. You need to have Node.js installed on your system.
  2. You'll also need the axios and form-data libraries:
npm install axios form-data
  1. A valid Bearer token (JWT) for authentication.
  2. A file to convert to PDF.

Endpoint

POST /documents/convert-to-pdf

Request

The request must be sent as multipart/form-data with the file attached.

Code Snippet

Use the following code snippet to convert a file to PDF. Replace <accessToken> with your API access token and update the file path.

const axios = require("axios");
const FormData = require("form-data");
const fs = require("fs");

const filePath = "path/to/your/document.docx";
const form = new FormData();
form.append("files", fs.createReadStream(filePath));

const config = {
method: "post",
url: "https://public-api.sign.stewart.com/documents/convert-to-pdf",
headers: {
Authorization: "Bearer <accessToken>",
...form.getHeaders(),
},
data: form,
responseType: "arraybuffer",
};

axios(config)
.then(function (response) {
fs.writeFileSync("converted-document.pdf", response.data);
console.log("Document converted to PDF successfully.");
})
.catch(function (error) {
console.log(error.response?.data?.toString() || error.message);
});

Successful Response

On success, the API returns the converted PDF file as binary data.

Error Responses

StatusReason
400 Bad RequestInvalid file or unsupported format
401 UnauthorizedInvalid or expired Bearer token

Steps to Execute the Code Snippet

  1. Create a new file named convert_to_pdf.js.
  2. Copy and paste the code snippet provided above into the new file.
  3. Replace <accessToken> with your API access token and update the file path.
  4. Save the file and open a terminal or command prompt.
  5. Navigate to the folder where you saved the convert_to_pdf.js file.
  6. Run the following command to execute the script:
node convert_to_pdf.js

If the request is successful, the converted PDF will be saved as converted-document.pdf in the current directory. In case of an error, the error details will be displayed instead.

Additional Resources

For more information about the eSignature API, visit the API reference.

If you need further assistance or have questions, feel free to reach out to our support team.