Skip to content

Commit 1278cbf

Browse files
committed
Convert examples/node/pdf2svg.js to await/async #14125
1 parent 52fce0d commit 1278cbf

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

examples/node/pdf2svg.js

+52-55
Original file line numberDiff line numberDiff line change
@@ -70,73 +70,70 @@ ReadableSVGStream.prototype._read = function () {
7070
};
7171

7272
// Streams the SVG element to the given file path.
73-
function writeSvgToFile(svgElement, filePath) {
73+
async function writeSvgToFile(svgElement, filePath) {
7474
let readableSvgStream = new ReadableSVGStream({
7575
svgElement,
7676
});
7777
const writableStream = fs.createWriteStream(filePath);
78-
return new Promise(function (resolve, reject) {
79-
readableSvgStream.once("error", reject);
80-
writableStream.once("error", reject);
81-
writableStream.once("finish", resolve);
82-
readableSvgStream.pipe(writableStream);
83-
}).catch(function (err) {
78+
try {
79+
await readableSvgStream.once("error");
80+
await writableStream.once("error");
81+
await writableStream.once("finish");
82+
await readableSvgStream.pipe(writableStream);
83+
} catch (err) {
8484
readableSvgStream = null; // Explicitly null because of v8 bug 6512.
8585
writableStream.end();
8686
throw err;
87-
});
87+
}
8888
}
8989

90-
// Will be using promises to load document, pages and misc data instead of
90+
// Will be using async/await to load document, pages and misc data instead of
9191
// callback.
92-
const loadingTask = pdfjsLib.getDocument({
93-
data,
94-
cMapUrl: CMAP_URL,
95-
cMapPacked: CMAP_PACKED,
96-
fontExtraProperties: true,
97-
});
98-
loadingTask.promise
99-
.then(function (doc) {
100-
const numPages = doc.numPages;
101-
console.log("# Document Loaded");
102-
console.log("Number of Pages: " + numPages);
92+
const loadingTask = async function () {
93+
const loadTask = pdfjsLib.getDocument({
94+
data,
95+
cMapUrl: CMAP_URL,
96+
cMapPacked: CMAP_PACKED,
97+
fontExtraProperties: true,
98+
});
99+
const doc = await loadTask.promise;
100+
const numPages = await doc.numPages;
101+
console.log("# Document Loaded");
102+
console.log("Number of Pages: " + numPages);
103+
console.log();
104+
105+
let finalResult;
106+
const loadPage = async function (pageNum) {
107+
const page = await doc.getPage(pageNum);
108+
console.log("# Page " + pageNum);
109+
const viewport = page.getViewport({ scale: 1.0 });
110+
console.log("Size: " + viewport.width + "x" + viewport.height);
103111
console.log();
104112

105-
let lastPromise = Promise.resolve(); // will be used to chain promises
106-
const loadPage = function (pageNum) {
107-
return doc.getPage(pageNum).then(function (page) {
108-
console.log("# Page " + pageNum);
109-
const viewport = page.getViewport({ scale: 1.0 });
110-
console.log("Size: " + viewport.width + "x" + viewport.height);
111-
console.log();
112-
113-
return page.getOperatorList().then(function (opList) {
114-
const svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
115-
svgGfx.embedFonts = true;
116-
return svgGfx.getSVG(opList, viewport).then(function (svg) {
117-
return writeSvgToFile(svg, getFilePathForPage(pageNum)).then(
118-
function () {
119-
console.log("Page: " + pageNum);
120-
},
121-
function (err) {
122-
console.log("Error: " + err);
123-
}
124-
);
125-
});
126-
});
127-
});
128-
};
113+
const opList = await page.getOperatorList();
114+
const svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
115+
svgGfx.embedFonts = true;
116+
const svg = await svgGfx.getSVG(opList, viewport);
117+
let result;
129118

130-
for (let i = 1; i <= numPages; i++) {
131-
lastPromise = lastPromise.then(loadPage.bind(null, i));
119+
try {
120+
result = await writeSvgToFile(svg, getFilePathForPage(pageNum));
121+
console.log("Page: " + pageNum);
122+
} catch (err) {
123+
console.log("Error: " + err);
132124
}
133-
return lastPromise;
134-
})
135-
.then(
136-
function () {
137-
console.log("# End of Document");
138-
},
139-
function (err) {
140-
console.error("Error: " + err);
125+
return result;
126+
};
127+
128+
try {
129+
for (let i = 1; i <= numPages; i++) {
130+
finalResult = loadPage.bind(null, i);
141131
}
142-
);
132+
console.log("# End of Document");
133+
} catch (err) {
134+
console.error("Error: " + err);
135+
}
136+
return finalResult;
137+
};
138+
139+
loadingTask();

0 commit comments

Comments
 (0)