How to add text and images to PDF in JavaScript using PDF.co Web API

The tutorial shows how to add text and images to PDF in JavaScript

Sample source code below will show you how to cope with a difficult task like add text and images to PDF in JavaScript. Want to add text and images to PDF in your JavaScript app? PDF.co Web API is designed for it. PDF.co Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

JavaScript code samples for JavaScript developers help to speed up coding of your application when using PDF.co Web API. Just copy and paste the code into your JavaScript application’s code and follow the instruction. Implementing JavaScript application typically includes multiple stages of the software development so even if the functionality works please test it with your data and the production environment.

Trial version of PDF.co Web API can be downloaded for free from our website. It also includes source code samples for JavaScript and other programming languages.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

AddImageByFindingTargetCoordinates.js
      
var https = require("https"); var path = require("path"); var fs = require("fs"); // `request` module is required for file upload. // Use "npm install request" command to install. var request = require("request"); // The authentication key (API Key). // Get your own by registering at https://app.pdf.co/documentation/api const API_KEY = "***********************************"; // Direct URL of source PDF file. const SourceFileUrl = "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-edit/sample.pdf"; // Search string. const SearchString = 'Your Company Name'; // Prepare URL for PDF text search API call. // See documentation: https://app.pdf.co/documentation/api/1.0/pdf/find.html var queryFindText = `https://api.pdf.co/v1/pdf/find`; let reqOptionsFindText = { uri: queryFindText, headers: { "x-api-key": API_KEY }, formData: { url: SourceFileUrl, searchString: SearchString } }; // Send request request.get(reqOptionsFindText, function (error, responseFindText, bodyFindText) { if (error) { return console.error("Error: ", error); } // Parse JSON response let dataFindText = JSON.parse(bodyFindText); if (dataFindText.body.length > 0) { var element = dataFindText.body[0]; console.log("Found text " + element["text"] + " at coordinates " + element["left"] + ", " + element["top"]); // Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'. const Pages = ""; // PDF document password. Leave empty for unprotected documents. const Password = ""; // Destination PDF file name const DestinationFile = "./result.pdf"; // Image params const Type = "image"; const X = 450; const Y = +element["top"]; const Width = 119; const Height = 32; const ImageUrl = "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-edit/logo.png"; // * Add image * // Prepare request to `PDF Edit` API endpoint var queryPath = `/v1/pdf/edit/add?name=${path.basename(DestinationFile)}&password=${Password}&pages=${Pages}&url=${SourceFileUrl}&type=${Type}&x=${X}&y=${Y}&width=${Width}&height=${Height}&urlimage=${ImageUrl}`; var reqOptions = { host: "api.pdf.co", path: encodeURI(queryPath), headers: { "x-api-key": API_KEY } }; // Send request https.get(reqOptions, (response) => { response.on("data", (d) => { // Parse JSON response var data = JSON.parse(d); if (data.error == false) { // Download the PDF file var file = fs.createWriteStream(DestinationFile); https.get(data.url, (response2) => { response2.pipe(file).on("close", () => { console.log(`Generated PDF file saved to '${DestinationFile}' file.`); }); }); } else { // Service reported error console.log(data.message); } }); }).on("error", (e) => { // Request error console.error(e); }); } else { console.error("No result found."); } });

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Related Samples: