Repair PDF in Node.js
Contents
[
Hide
]
Aspose.PDF for Node.js allows high-quality PDF repair. The PDF file may not open for any reason, regardless of the program or browser. In some cases the document can be restored, try the following code and see for yourself. In case you want to repair PDF document, you can use AsposePdfRepair function. Please check following code snippet in order to repair PDF file in Node.js environment.
CommonJS:
- Call
requireand importasposepdfnodejsmodule asAsposePdfvariable. - Specify the name of the PDF file that will be repaired.
- Call
AsposePdfas Promise and perform the operation for repairing file. Receive the object if successful. - Call the function AsposePdfRepair.
- Repair the PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfRepair.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Repair a PDF-file and save the "ResultPdfRepair.pdf"*/
const json = AsposePdfModule.AsposePdfRepair(pdf_file, "ResultPdfRepair.pdf");
console.log("AsposePdfRepair => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- Import the
asposepdfnodejsmodule. - Specify the name of the PDF file that will be repaired.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfRepair.
- Repair the PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfRepair.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Repair a PDF-file and save the "ResultPdfRepair.pdf"*/
const json = AsposePdfModule.AsposePdfRepair(pdf_file, "ResultPdfRepair.pdf");
console.log("AsposePdfRepair => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);