> ## Documentation Index
> Fetch the complete documentation index at: https://support.stuut.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Invoice PDFs (RESTlet)

> Deploy a RESTlet so Stuut can attach NetSuite invoice PDFs to customer emails.

NetSuite's REST API doesn't return invoice PDFs, so Stuut uses a small RESTlet in your account to render them. Once it's deployed, Stuut downloads each invoice's PDF during sync and can attach it to collection emails.

This setup is optional. Skip it if you don't want invoice PDFs on Stuut emails.

## Before you begin

* Start with the [NetSuite connection setup](https://support.stuut.ai/technical-guides/api-integrations/netsuite). You can deploy the RESTlet before or after you finish connecting; the setup has an **Optional settings** screen where the IDs can be entered.
* Make sure your Integration Record has the **RESTlets** scope checked (step 5 of the connection setup). If you connected with an interactive OAuth 2.0 login and add the scope later, re-authorize the connection in Stuut afterwards.

## 1. Enable SuiteScript

1. Go to **Setup → Company → Enable Features** and open the **SuiteCloud** tab.
2. Check **Client SuiteScript** and **Server SuiteScript**.
3. Click **Save**.

## 2. Upload the script

Save the following as `invoice_pdf_restlet.js`:

```javascript invoice_pdf_restlet.js theme={null}
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 *
 * Invoice PDF Generator RESTlet
 *
 * Returns invoice PDF as base64-encoded content via OAuth 2.0 authenticated requests.
 *
 * Usage:
 *   GET https://{account_id}.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script={script_id}&deploy={deploy_id}&id={invoice_id}
 *   Headers: Authorization: Bearer {access_token}
 *
 * Response:
 *   { success: true, content: "base64_pdf_content", filename: "invoice_1.pdf", contentType: "application/pdf" }
 */
define(['N/render', 'N/log'], function (render, log) {
    function get(requestParams) {
        try {
            var id = requestParams && requestParams.id;
            if (!id) {
                return { success: false, error: 'Missing invoice ID parameter' };
            }

            log.debug('Generate PDF', 'Invoice ID: ' + id);

            // Generate PDF from invoice transaction
            var pdfFile = render.transaction({
                entityId: parseInt(id, 10),
                printMode: render.PrintMode.PDF
            });

            // Get contents as base64 string (render.transaction already returns base64 for PDFs)
            // Use String() to ensure JSON serialization works correctly
            var base64 = String(pdfFile.getContents() || '');

            log.debug('PDF Generated', 'Size: ' + base64.length + ' bytes');

            return {
                success: true,
                filename: 'invoice_' + id + '.pdf',
                contentType: 'application/pdf',
                content: base64
            };
        } catch (e) {
            log.error('PDF Generation Failed', JSON.stringify({
                name: e.name,
                message: e.message
            }));
            return {
                success: false,
                error: e && e.message ? e.message : String(e)
            };
        }
    }

    return { get: get };
});
```

Then in NetSuite:

1. Go to **Customization → Scripting → Scripts → New**.
2. Upload `invoice_pdf_restlet.js` as the script file and click **Create Script Record**.
3. Set **Name** to `Invoice PDF Generator` and **ID** to `_invoice_pdf_restlet`. NetSuite adds the `customscript` prefix, so the full script ID is `customscript_invoice_pdf_restlet`.
4. Save.

## 3. Deploy the script

On the script record, click **Deploy Script** and set:

* **Title:** `Invoice PDF Deployment`
* **ID:** `_invoice_pdf`. The full deployment ID becomes `customdeploy_invoice_pdf`.
* **Status:** Released
* **Audience → Roles:** include the `Stuut Integration` role

Save the deployment.

> [!NOTE]
>
> The PDF uses the invoice's standard print template, so it matches what you'd get from **Print** in NetSuite.

## 4. Enter the RESTlet IDs in Stuut

Enter both IDs on the NetSuite connection in Stuut:

* **PDF RESTlet script ID:** `customscript_invoice_pdf_restlet`
* **PDF RESTlet deployment ID:** `customdeploy_invoice_pdf`

If you're still in the connection setup, enter them on the **Optional settings** screen and click **Save and continue**. Otherwise, go to **Settings → Connectors**, open your NetSuite connection, enter them on the **Settings** tab, and save.

Stuut starts downloading invoice PDFs on the next sync.

If you used different IDs in steps 2 and 3, enter yours instead. Both values are the full IDs, including the `customscript` and `customdeploy` prefixes.

## Troubleshooting

> [accordion] No PDFs appear after the next sync
>
> Check that the deployment's **Status** is **Released**, the `Stuut Integration` role is in its **Audience**, and the Integration Record has the **RESTlets** scope. Then confirm both IDs in Stuut match the script and deployment records exactly.

> [accordion] Some PDFs arrive late during the first import
>
> NetSuite limits how many RESTlet calls one user can make at the same time. When the RESTlet reports it's busy, Stuut waits and tries again on a later run, so a large first import can take a while to fill in every PDF.

> [accordion] Seeing script errors in NetSuite
>
> The script logs failures under **Customization → Scripting → Script Deployments → Invoice PDF Deployment → Execution Log**. Share the error with your Stuut team.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.