A simple Node.js program to render a URL as PDF using the ScreenshotOne API.
- Node.js
- npm
- ScreenshotOne API access key
- Clone this repository or download the source code.
git clone https://github.com/screenshotone/examples.git- Navigate to the project directory.
cd examples/nodejs/url-to-pdf- Install the dependencies:
npm install- Create a
.envfile in the project root directory and add your ScreenshotOne API access key:
SCREENSHOTONE_ACCESS_KEY=your_access_key
You can get your API access key by signing up at ScreenshotOne.
Run the program with a URL to convert it to PDF:
node index.js https://example.comThis will generate a PDF file named output.pdf in the project directory.
You can specify a custom output path for the PDF file:
node index.js https://example.com /path/to/output.pdfThe application includes a built-in retry mechanism that will automatically retry failed requests up to 3 times with an exponential backoff delay between retries. This helps handle temporary network issues or API rate limits.
You can modify the retry parameters in the main() function in index.js:
const maxRetries = 3; // Maximum number of retry attempts
const retryDelay = 2000; // Initial delay between retries in millisecondsYou can modify the options object in the main() function in index.js to customize the PDF generation. For a complete list of options, refer to the ScreenshotOne API documentation.