How to Replace Text from PDF in JavaScript with PDF.co Web API
In this tutorial, we’ll be looking at how to replace text within PDF using PDF.co and NodeJs. PDF manipulation is complicated stuff, however after this article you will realize that it’s very much simplified by PDF.co.
PDF.co Web API is the REST API that provides a set of data extraction functions, and tools for document manipulation, splitting, and merging of PDF files. Includes built-in OCR, images recognition, and can generate and read barcodes from images, scans, and PDFs.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
Without wasting any time let’s jump into the source code and its output. We’ll analyze the source code later.
Source Code
var https = require("https"); var path = require("path"); var fs = require("fs"); // The authentication key (API Key). // Get your own by registering at https://app.pdf.co const API_KEY = "******************************"; // Direct URL of source PDF file. // You can also upload your own file into PDF.co and use it as url. Check "Upload File" samples for code snippets: https://github.com/bytescout/pdf-co-api-samples/tree/master/File%20Upload/ const SourceFileUrl = "https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-to-text/sample.pdf"; // PDF document password. Leave empty for unprotected documents. const Password = ""; // Destination PDF file name const DestinationFile = "./result.pdf"; // Prepare request to `Replace Text from PDF` API endpoint var queryPath = `/v1/pdf/edit/replace-text`; // JSON payload for api request var jsonPayload = JSON.stringify({ name: path.basename(DestinationFile), password: Password, url: SourceFileUrl, searchString: 'Your Company Name', replaceString: 'XYZ LLC' }); var reqOptions = { host: "api.pdf.co", method: "POST", path: queryPath, headers: { "x-api-key": API_KEY, "Content-Type": "application/json", "Content-Length": Buffer.byteLength(jsonPayload, 'utf8') } }; // Send request var postRequest = https.request(reqOptions, (response) => { response.on("data", (d) => { // Parse JSON response var data = JSON.parse(d); if (data.error == false) { // Download PDF file var file = fs.createWriteStream(DestinationFile); https.get(data.url, (response2) => { response2.pipe(file) .on("close", () => { console.log(`Generated PDF file saved as "${DestinationFile}" file.`); }); }); } else { // Service reported error console.log(data.message); } }); }).on("error", (e) => { // Request error console.log(e); }); // Write request data postRequest.write(jsonPayload); postRequest.end();
Output
Source Code Analysis
The source code is pretty straightforward. Initially, we’re importing packages that are necessary for making API requests and working with files. The next step is to make a request and handle its output.
The PDF.co API endpoint “/v1/pdf/edit/replace-text” is used to replace the text within PDF. This Endpoint can also replace text based on the regex expression specified. Please refer to official documentation to know more about this endpoint.
The input request consist of parameters such as URL of input file, search string, replacement string, etc. For cases when we want to use a password-protected PDF, we need to pass PDF file’s password as a parameter.
// JSON payload for api request var jsonPayload = JSON.stringify({ name: path.basename(DestinationFile), password: Password, url: SourceFileUrl, searchString: 'Your Company Name', replaceString: 'XYZ LLC' });
Another important aspect to note is that we are passing the PDF.co API Key in the request header. Now, this API key is required to authenticate your PDF.co request. You will get the PDF.co API key upon signing up with PDF.co.
headers: { "x-api-key": API_KEY, ...
To get most out of this article, play with source code in your machine. Here’s a GitHub link for the full source code.
Thank You!
Video Tutorial
ON-PREMISE OFFLINE SDK
See also:
ON-DEMAND REST WEB API
Get Your API Key
See also:
PDF-co-Web-API-JavaScript-Replace-Text-From-File-(Node-js).pdf