Convert examples/node/pdf2png/pdf2png.js to await/async

This commit is contained in:
Jane-Kotovich 2021-10-14 13:26:10 +10:00
parent f6d9d91965
commit 37d90ec378

View File

@ -70,12 +70,13 @@ const loadingTask = pdfjsLib.getDocument({
cMapPacked: CMAP_PACKED,
standardFontDataUrl: STANDARD_FONT_DATA_URL,
});
loadingTask.promise
.then(function (pdfDocument) {
console.log("# PDF document loaded.");
(async function () {
try {
const pdfDocument = await loadingTask.promise;
console.log("# PDF document loaded.");
// Get the first page.
pdfDocument.getPage(1).then(function (page) {
const page = await pdfDocument.getPage(1);
// Render the page on a Node canvas with 100% scale.
const viewport = page.getViewport({ scale: 1.0 });
const canvasFactory = new NodeCanvasFactory();
@ -90,7 +91,7 @@ loadingTask.promise
};
const renderTask = page.render(renderContext);
renderTask.promise.then(function () {
await renderTask.promise;
// Convert the canvas to an image buffer.
const image = canvasAndContext.canvas.toBuffer();
fs.writeFile("output.png", image, function (error) {
@ -102,9 +103,7 @@ loadingTask.promise
);
}
});
});
});
})
.catch(function (reason) {
} catch (reason) {
console.log(reason);
});
}
})();