mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 17:30:09 +02:00
Convert examples/node/pdf2png/pdf2png.js to await/async
This commit is contained in:
parent
f6d9d91965
commit
37d90ec378
1 changed files with 32 additions and 33 deletions
|
@ -70,12 +70,13 @@ const loadingTask = pdfjsLib.getDocument({
|
||||||
cMapPacked: CMAP_PACKED,
|
cMapPacked: CMAP_PACKED,
|
||||||
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
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.
|
// 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.
|
// Render the page on a Node canvas with 100% scale.
|
||||||
const viewport = page.getViewport({ scale: 1.0 });
|
const viewport = page.getViewport({ scale: 1.0 });
|
||||||
const canvasFactory = new NodeCanvasFactory();
|
const canvasFactory = new NodeCanvasFactory();
|
||||||
|
@ -90,7 +91,7 @@ loadingTask.promise
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderTask = page.render(renderContext);
|
const renderTask = page.render(renderContext);
|
||||||
renderTask.promise.then(function () {
|
await renderTask.promise;
|
||||||
// Convert the canvas to an image buffer.
|
// Convert the canvas to an image buffer.
|
||||||
const image = canvasAndContext.canvas.toBuffer();
|
const image = canvasAndContext.canvas.toBuffer();
|
||||||
fs.writeFile("output.png", image, function (error) {
|
fs.writeFile("output.png", image, function (error) {
|
||||||
|
@ -102,9 +103,7 @@ loadingTask.promise
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
} catch (reason) {
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(function (reason) {
|
|
||||||
console.log(reason);
|
console.log(reason);
|
||||||
});
|
}
|
||||||
|
})();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue