How to add text and images to PDF in JavaScript with Web API

How to add text and images to PDF in JavaScript

These sample source codes on this page below are demonstrating how to add text and images to PDF in JavaScript. Want to add text and images to PDF in your JavaScript app? Web API is designed for it. 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 Web API. Just copy and paste the code into your JavaScript application’s code and follow the instruction. Enjoy writing a code with ready-to-use sample codes in JavaScript.

ByteScout free trial version is available for download from our website. It includes all these programming tutorials along with source code samples.

Try Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

var https = require("https"); var path = require("path"); var fs = require("fs"); // The authentication key (API Key). // Get your own by registering at const API_KEY = "***********************************"; // Direct URL of source PDF file. const SourceFileUrl = ""; // 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 = 400; const Y = 20; const Width = 119; const Height = 32; const ImageUrl = ""; // * 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: "", 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); });

Try Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)



Get 60 Day Free Trial or Visit Web API Home Page

Explore Web API Documentation

Get Web API Free Training


Get Your Free API Key

Explore Web API Documentation

Related Samples: