Open and save PDF files in JavaScript PDF Library
24 Jul 20265 minutes to read
The JavaScript PDF Library enables you to load existing PDF documents, modify their content, and save the updated document. This guide demonstrates the complete workflow, from loading and editing a PDF to saving and disposing of document resources.
Opening an existing PDF document
Open an existing PDF document by passing the PDF data to the PdfDocument constructor. The constructor accepts PDF data as a Base64-encoded string or a Uint8Array, and supports additional optional parameters for password and permissions. The following subsections demonstrate each data format.
Using Base64 String
Provide the PDF data as a Base64-encoded string to the PdfDocument constructor.
// Sample Base64-encoded PDF data
let data: string = 'JVBERi0xLjcNJeLjz9MNCjEyNSAw...........TU3MTQNCiUlRU9GDQo=';
// Load an existing PDF document from a Base64 string
let document: PdfDocument = new PdfDocument(data);var data = 'JVBERi0xLjcNJeLjz9MNCjEyNSAw...........TU3MTQNCiUlRU9GDQo=';
// Load an existing PDF document from data as Base64 string
var document = new ej.pdf.PdfDocumentPdfDocument(data);
// Load an existing PDF document from a Base64 string
var document = new ej.pdf.PdfDocument(data);Using Uint8Array
Open an existing PDF document using the PdfDocument class with the specified PDF data as Uint8Array.
// Sample Uint8Array data
let binaryData: Uint8Array = Uint8Array.from(data);
// Load an existing PDF document from a Uint8Array
let document: PdfDocument = new PdfDocument(binaryData);var binaryData = Uint8Array.from(data);
// Load an existing PDF document from a Uint8Array
var document = new ej.pdf.PdfDocument(binaryData);Opening an encrypted PDF document with password
Open an encrypted PDF document by providing the correct password to the PdfDocument constructor. The library supports standard PDF encryption algorithms including AES 128/256-bit and RC4 40/128-bit.
// Load an encrypted PDF document with a user password
let document: PdfDocument = new PdfDocument(data, 'password');// Load an encrypted PDF document with a user password
var document = new ej.pdf.PdfDocument(data, 'password');Saving and downloading a PDF document in the browser
Save and download the PDF document directly from the browser by passing a filename to the save method. The browser triggers a file download with the specified name.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// To-Do: perform manipulation on the document
// Save and download the PDF document with the specified filename
document.save('output.pdf');// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// To-Do: perform manipulation on the document
// Save and download the PDF document with the specified filename
document.save('output.pdf');Saving a PDF document to a byte array
Save the modified PDF document to memory as a Uint8Array using the parameterless save overload. This is useful when you want to forward the PDF to a server, store it in a database, or process it further before downloading.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// To-Do: perform manipulation on the document
// Save the PDF document and obtain a Uint8Array
let bytes: Uint8Array = document.save();// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// To-Do: perform manipulation on the document
// Save the PDF document and obtain a Uint8Array
var bytes = document.save();Closing a PDF document
After every load and save operation described above, you must call the destroy method on the PdfDocument instance to release the memory held by the PDF document instance. Failing to do so can lead to memory leaks, especially in long-running applications or when processing many documents in sequence.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// To-Do: perform manipulation on the document
// Save the PDF document
document.save('output.pdf');
// Destroy the document
document.destroy();// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// To-Do: perform manipulation on the document
// Save the PDF document
document.save('output.pdf');
// Destroy the document
document.destroy();