This sample project helps you get started with the PDF Tools Node.js SDK.
The sample JS scripts illustrate how to perform PDF-related actions (such as converting to and from the PDF format) using the SDK. Please note that the PDF Tools Node.js SDK supports only server side use cases.
The sample application has the following requirements:
- Node.js : Version 10.13 or above. Node installation instructions can be found here.
The credentials file and corresponding private key file for the samples is pdftools-api-credentials.json
and private.key
respectively. Before the samples can be run, replace both the files with the ones present in the downloaded zip file at
the end of creation of credentials via Get Started workflow.
The SDK also supports providing the authentication credentials at runtime, without storing them in a config file. Please refer this section to know more.
If you receive ServiceUsageError during the Samples run, it means that trial credentials have exhausted their usage quota. Please contact us to get paid credentials.
Run the following command to build the project:
npm install
Note that the PDF Tools SDK is listed as a dependency in the package.json and will be downloaded automatically.
For logging, this SDK uses the log4js API .
Upon running, the SDK searches for a file config/pdftools-sdk-log4js-config.json
in the working directory, and reads the
logging properties from there. If no configuration file is provided, default logging, i.e. logging INFO logs to the console is enabled. The clients can change the logging settings as per their needs.
The following sub-sections describe how to run the samples. Prior to running the samples, check that the configuration file is set up as described above and that the project has been built.
The samples code is available under the src
folder. Test
files used by the samples can be found in the resources
folder. When executed, all samples create an output
child folder under the project root directory to store their results.
These samples illustrate how to convert files of some formats to PDF. Refer the sdk documentation of create-pdf-operation.js to see the list of all supported media types which can be converted to PDF.
The sample script create-pdf-from-docx.js
creates a PDF file from a DOCX file.
node src/createpdf/create-pdf-from-docx.js
The sample script create-pdf-from-docx-stream.js
creates a PDF file from a readable DOCX stream.
node src/createpdf/create-pdf-from-docx-stream.js
The sample script create-pdf-from-docx-to-output-stream.js
creates a PDF file from a DOCX file. Instead of saving the result to a
local file, it writes the result to an output stream.
node src/createpdf/create-pdf-from-docx-to-output-stream.js
The sample script create-pdf-with-inmemory-auth-credentials.js
highlights how to provide in-memory auth credentials
for performing an operation. This enables the clients to fetch the credentials from a secret server during runtime,
instead of storing them in a file.
Before running the sample, authentication credentials need to be updated as per the instructions in the script.
node src/createpdf/create-pdf-with-inmemory-auth-credentials.js
The sample script create-pdf-with-custom-timeouts
highlights how to provide the custom value for connection timeout and read timeout.
node src/createpdf/create-pdf-with-custom-timeouts.js
The sample script create-pdf-from-pptx.js
creates a PDF file from a PPTX file.
node src/createpdf/create-pdf-from-pptx.js
The sample script create-pdf-from-static-html.js
creates a PDF file from a zip file containing the input HTML file and its resources.
Please refer the sdk documentation of create-pdf-operation.js to see instructions on the structure of the zip file.
node src/createpdf/create-pdf-from-static-html.js
The sample script create-pdf-from-dynamic-html.js
converts a zip file, containing the input HTML file and its resources, along with the input data to a PDF file. The input data is used by the javascript in the HTML file to manipulate the HTML DOM, thus effectively updating the source HTML file. This mechanism can be used to provide data to the template HTML dynamically and then, convert it into a PDF file.
node src/createpdf/create-pdf-from-dynamic-html.js
These samples illustrate how to export PDF files to other formats. Refer to the documentation of export-pdf-operation.js to see the list of supported export formats.
The sample script export-pdf-to-docx.js
converts a PDF file to a DOCX file.
node src/exportpdf/export-pdf-to-docx.js
The sample script export-pdf-to-jpeg.js
converts a PDF file's pages to JPEG images. Note that the output is a zip archive
containing the individual images.
node src/exportpdf/export-pdf-to-jpeg.js
These samples illustrate how to combine multiple PDF files into a single PDF file.
The sample script combine-pdf.js
combines multiple PDF files into a single PDF file. The combined PDF file contains all pages
of the source files.
node src/combinepdf/combine-pdf.js
The sample script combine-pdf-with-page-ranges.js
combines specific pages of multiple PDF files into into a single PDF file.
node src/combinepdf/combine-pdf-with-page-ranges.js
These samples illustrate how to apply OCR(Optical Character Recognition) to a PDF file and convert it to a searchable copy of your PDF. The supported input format is application/pdf.
The sample script ocr-pdf.js
converts a PDF file into a searchable PDF file.
node src/ocr/ocr-pdf.js
The sample script ocr-pdf-with-options.js
converts a PDF file to a searchable PDF file with maximum fidelity to the original image and default en-us locale. Refer to the documentation of ocr-options.js
to see the list of supported OCR locales and OCR types.
node src/ocr/ocr-pdf-with-options.js
The sample illustrates how to reduce the size of a PDF file.
The sample script compress-pdf.js
reduces the size of a PDF file.
node src/compresspdf/compress-pdf.js
The sample illustrates how to convert a PDF file into a Linearized (also known as "web optimized") PDF file. Such PDF files are optimized for incremental access in network environments.
The sample script linearize-pdf.js
optimizes the PDF file for a faster Web View.
node src/linearizepdf/linearize-pdf.js
The sample illustrates how to secure a PDF file with a password.
The sample script protect-pdf.js
converts a PDF file into a password protected PDF file.
node src/protectpdf/protect-pdf.js
The sample illustrates how to rotate pages in a PDF file.
The sample script rotate-pdf-pages.js
rotates specific pages in a PDF file.
node src/rotatepages/rotate-pdf-pages.js
The sample illustrates how to delete pages in a PDF file.
The sample script delete-pdf-pages.js
removes specific pages from a PDF file.
node src/deletepages/delete-pdf-pages.js
The sample illustrates how to reorder the pages in a PDF file.
The sample script reorder-pdf-pages.js
rearranges the pages of a PDF file according to the specified order.
node src/reorderpages/reorder-pdf-pages.js
The sample illustrates how to insert pages in a PDF file.
The sample script insert-pdf-pages.js
inserts pages of multiple PDF files into a base PDF file.
node src/insertpages/insert-pdf-pages.js
The sample illustrates how to replace pages of a PDF file.
The sample script replace-pdf-pages.js
replaces specific pages in a PDF file with pages from multiple PDF files.
node src/replacepages/replace-pdf-pages.js
This project is licensed under the MIT License. See LICENSE for more information.