SelectPdf cloud REST API is a platform independent PDF manipulation API. As a true REST API, it can be used with any language: .NET, Java, PHP, Python, Go, Ruby and many more. We are presenting today the dedicated Node.js client library for SelectPdf API.
Using the SelectPdf Online REST API Node.js client library you can easily take advance of the API features offered by SelectPdf:
HTML to PDF REST API – SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications.
PDF to TEXT REST API – SelectPdf Pdf To Text REST API is an online solution that lets you extract text from your PDF documents or search your PDF document for certain words.
PDF Merge REST API – SelectPdf Pdf Merge REST API is an online solution that lets you merge local or remote PDFs into a final PDF document.
All these APIs can be easily integrated with Node.js using the dedicated client library.
Installation
Install SelectPdf Node.js Client for Online API via npm.
OR
Download selectpdf-api-nodejs-client-1.4.0.zip, unzip it and run:
OR
Clone selectpdf-api-nodejs-client from Github and install the library.
npm install /path/to/selectpdf-api-nodejs-client-1.4.0
Get a trial key for SelectPdf online REST API
Once the library is installed, you need a key to be able to access the API.
GET A DEMO LICENSE KEY NOW
The free trial key for the online API is valid for 7 days and it includes 200 conversions.
Sample Code
The Nodejs client library makes accessing SelectPdf online REST API very easy. Here are a few samples that present the main features of the API. For details and full list of parameters access the SelectPdf API Nodejs documentation on GitHub or the individual pages of the APIs: HTML to PDF API or PDF to TEXT API or PDF Merge API.
Convert HTML to PDF in Nodejs
The following sample shows the main features of the HTML To PDF API. Comment/uncomment code to convert an url to file or memory or also convert raw HTML to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
var selectpdf = require('selectpdf'); console.log("This is SelectPdf-%s.", selectpdf.CLIENT_VERSION); try { var url = 'https://selectpdf.com'; var localFile = 'Test.pdf' var apiKey = 'Your API key here'; var client = new selectpdf.HtmlToPdfClient(apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ client // main properties .setPageSize('A4') // PDF page size .setPageOrientation('Portrait') // PDF page orientation .setMargins(0) // PDF page margins .setRenderingEngine('WebKit') // rendering engine .setConversionDelay(1) // conversion delay .setNavigationTimeout(30) // navigation timeout .setShowPageNumbers(false) // page numbers .setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // additional properties // .setUseCssPrint('True') // enable CSS media print // .setDisableJavascript('True') // disable javascript // .setDisableInternalLinks('True') // disable internal links // .setDisableExternalLinks('True') // disable external links // .setKeepImagesTogether('True') // keep images together // .setScaleImages('True') // scale images to create smaller pdfs // .setSinglePagePdf('True') // generate a single page PDF // .setUserPassword('password') // secure the PDF with a password // generate automatic bookmarks // .setPdfBookmarksSelectors('H1, H2') // create outlines (bookmarks) for the specified elements // .setViewerPageMode(1) // display outlines (bookmarks) in viewer ; console.log("Starting conversion ..."); // convert url to file client.convertUrlToFile(url, localFile, function(err, fileName) { if (err) return console.log("An error occurred: " + err); console.log("Finished! Result is in file '" + fileName + "'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); // convert url to memory /* client.convertUrl(url, function(err, pdf) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in variable 'pdf'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ // convert html string to file /* client.convertHtmlStringToFile('This is some <b>html</b>.', localFile, function(err, fileName) { if (err) return console.log("An error occurred: " + err); console.log("Finished! Result is in file '" + fileName + "'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ // convert html string to memory /* client.convertHtmlString('This is some <b>html</b>.', function(err, pdf) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in variable 'pdf'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ } catch (ex) { console.log("An error occurred: " + ex); } |
Convert HTML to PDF with custom header/footer in Nodejs
The following sample shows how to convert a web page to PDF and also setting a custom header or footer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
var selectpdf = require('selectpdf'); console.log("This is SelectPdf-%s.", selectpdf.CLIENT_VERSION); try { var url = 'https://selectpdf.com'; var localFile = 'Test.pdf' var apiKey = 'Your API key here'; var client = new selectpdf.HtmlToPdfClient(apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ client .setMargins(0) // PDF page margins .setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // header properties .setShowHeader('True') // display header // .setHeaderHeight(50) // header height // .setHeaderUrl(url) // header url .setHeaderHtml('This is the <b>HEADER</b>!!!!') // header html // footer properties .setShowFooter('True') // display footer // .setFooterHeight(60) // footer height // .setFooterUrl(url) // footer url .setFooterHtml('This is the <b>FOOTER</b>!!!!') // footer html // footer page numbers .setShowPageNumbers('True') // show page numbers in footer .setPageNumbersTemplate('{page_number} / {total_pages}') // page numbers template .setPageNumbersFontName('Verdana') // page numbers font name .setPageNumbersFontSize(12) // page numbers font size .setPageNumbersAlignment(2) // page numbers alignment (2-Center) ; console.log("Starting conversion ..."); // convert url to file client.convertUrlToFile(url, localFile, function(err, fileName) { if (err) return console.log("An error occurred: " + err); console.log("Finished! Result is in file '" + fileName + "'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); // convert url to memory /* client.convertUrl(url, function(err, pdf) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in variable 'pdf'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ // convert html string to file /* client.convertHtmlStringToFile('This is some <b>html</b>.', localFile, function(err, fileName) { if (err) return console.log("An error occurred: " + err); console.log("Finished! Result is in file '" + fileName + "'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ // convert html string to memory /* client.convertHtmlString('This is some <b>html</b>.', function(err, pdf) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in variable 'pdf'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ } catch (ex) { console.log("An error occurred: " + ex); } |
Extract text from PDF in Nodejs
The following sample shows how to extract the text from a PDF document using SelectPdf API. Comment/uncomment code to convert a local PDF or a PDF from a remote url to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
var selectpdf = require('selectpdf'); console.log("This is SelectPdf-%s.", selectpdf.CLIENT_VERSION); try { var testUrl = 'https://selectpdf.com/demo/files/selectpdf.pdf'; var testPdf = 'Input.pdf'; var localFile = 'Result.txt'; var apiKey = 'Your API key here'; var client = new selectpdf.PdfToTextClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ client .setStartPage(1) // start page (processing starts from here) .setEndPage(0) // end page (set 0 to process file til the end) .setOutputFormat(0) // set output format (0-Text or 1-HTML) ; console.log('Starting pdf to text ...'); // convert local pdf to local text file client.getTextFromFileToFile(testPdf, localFile, function(err, fileName) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in file '" + fileName + "'. Number of pages processed: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); // extract text from local pdf to memory /* client.getTextFromFile(testPdf, function(err, text) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in variable 'text'. Number of pages processed: " + client.getNumberOfPages()); console.log(text); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ } catch (ex) { console.log("An error occurred: " + ex); } |
Search for text in PDF using Nodejs
The following sample shows how to search a PDF document for a specific text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
var selectpdf = require('selectpdf'); console.log("This is SelectPdf-%s.", selectpdf.CLIENT_VERSION); try { var testUrl = 'https://selectpdf.com/demo/files/selectpdf.pdf'; var testPdf = 'Input.pdf'; var apiKey = 'Your API key here'; var client = new selectpdf.PdfToTextClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ client .setStartPage(1) // start page (processing starts from here) .setEndPage(0) // end page (set 0 to process file til the end) ; console.log('Starting search pdf ...'); // search local pdf client.searchFile(testPdf, 'pdf', false, false, function(err, results) { if (err) return console.error("An error occurred: " + err); console.log("Search results: " + JSON.stringify(results) +".\nSearch results count: " + results.length + "."); console.log("Finished! Number of pages processed: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); // search pdf from public url /* client.searchUrl(testUrl, 'pdf', false, false, function(err, results) { if (err) return console.error("An error occurred: " + err); console.log("Search results: " + JSON.stringify(results) +".\nSearch results count: " + results.length + "."); console.log("Finished! Number of pages processed: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ } catch (ex) { console.log("An error occurred: " + ex); } |
Merge PDFs using Nodejs
The following sample shows how merge several PDF documents into a final file. The source PDFs can be local files or PDFs from remote urls. The final PDF can be retrieved in memory or saved to a local file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
var selectpdf = require('selectpdf'); console.log("This is SelectPdf-%s.", selectpdf.CLIENT_VERSION); try { var testUrl = 'https://selectpdf.com/demo/files/selectpdf.pdf'; var testPdf = 'Input.pdf'; var localFile = 'Result.pdf'; var apiKey = 'Your API key here'; var client = new selectpdf.PdfMergeClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-merge-api/ client // specify the pdf files that will be merged (order will be preserved in the final pdf) .addFile(testPdf) // add PDF from local file .addUrlFile(testUrl) // add PDF From public url //.addFile(testPdf, "pdf_password") // add PDF (that requires a password) from local file //.addUrlFile(testUrl, "pdf_password") // add PDF (that requires a password) from public url ; console.log('Starting pdf merge ...'); // merge pdfs to local file client.saveToFile(localFile, function(err, fileName) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in file '" + fileName + "'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); // merge pdfs to memory /* client.save( function(err, pdf) { if (err) return console.error("An error occurred: " + err); console.log("Finished! Result is in variable 'pdf'. Number of pages: " + client.getNumberOfPages()); var usageClient = new selectpdf.UsageClient(apiKey); usageClient.getUsage(false, function(err2, data) { if (err2) return console.error("An error occurred getting the usage info: " + err2); console.log("Conversions remained this month: " + data["available"] + ". Usage: " + JSON.stringify(data)); }); } ); */ } catch (ex) { console.log("An error occurred: " + ex); } |
The above Node.js samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-nodejs-client/tree/main/samples.