2017-03-03 01:48:33 +05:30
|
|
|
/* Copyright 2017 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-10-14 11:24:56 +02:00
|
|
|
import fs from "fs";
|
|
|
|
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
|
2017-03-03 01:48:33 +05:30
|
|
|
|
2020-06-29 13:18:51 +02:00
|
|
|
// Some PDFs need external cmaps.
|
2021-03-12 17:08:17 +01:00
|
|
|
const CMAP_URL = "../../../node_modules/pdfjs-dist/cmaps/";
|
|
|
|
const CMAP_PACKED = true;
|
2017-03-03 01:48:33 +05:30
|
|
|
|
2020-12-10 17:32:18 -08:00
|
|
|
// Where the standard fonts are located.
|
|
|
|
const STANDARD_FONT_DATA_URL =
|
|
|
|
"../../../node_modules/pdfjs-dist/standard_fonts/";
|
|
|
|
|
2020-06-29 13:18:51 +02:00
|
|
|
// Loading file from file system into typed array.
|
2021-03-12 17:08:17 +01:00
|
|
|
const pdfPath =
|
2020-06-29 13:18:51 +02:00
|
|
|
process.argv[2] || "../../../web/compressed.tracemonkey-pldi-09.pdf";
|
2021-03-12 17:08:17 +01:00
|
|
|
const data = new Uint8Array(fs.readFileSync(pdfPath));
|
2017-03-03 01:48:33 +05:30
|
|
|
|
2018-06-01 12:52:29 +02:00
|
|
|
// Load the PDF file.
|
2023-10-14 11:24:56 +02:00
|
|
|
const loadingTask = getDocument({
|
2021-01-22 17:38:26 +01:00
|
|
|
data,
|
2020-06-29 13:18:51 +02:00
|
|
|
cMapUrl: CMAP_URL,
|
|
|
|
cMapPacked: CMAP_PACKED,
|
2020-12-10 17:32:18 -08:00
|
|
|
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
2020-06-29 13:18:51 +02:00
|
|
|
});
|
2017-03-03 01:48:33 +05:30
|
|
|
|
2023-10-14 11:24:56 +02:00
|
|
|
try {
|
|
|
|
const pdfDocument = await loadingTask.promise;
|
|
|
|
console.log("# PDF document loaded.");
|
|
|
|
// Get the first page.
|
|
|
|
const page = await pdfDocument.getPage(1);
|
|
|
|
// Render the page on a Node canvas with 100% scale.
|
2024-09-23 10:22:39 +02:00
|
|
|
const canvasFactory = pdfDocument.canvasFactory;
|
2023-10-14 11:24:56 +02:00
|
|
|
const viewport = page.getViewport({ scale: 1.0 });
|
|
|
|
const canvasAndContext = canvasFactory.create(
|
|
|
|
viewport.width,
|
|
|
|
viewport.height
|
|
|
|
);
|
|
|
|
const renderContext = {
|
|
|
|
canvasContext: canvasAndContext.context,
|
|
|
|
viewport,
|
|
|
|
};
|
2017-03-03 01:48:33 +05:30
|
|
|
|
2023-10-14 11:24:56 +02:00
|
|
|
const renderTask = page.render(renderContext);
|
|
|
|
await renderTask.promise;
|
|
|
|
// Convert the canvas to an image buffer.
|
2024-11-08 11:29:24 +01:00
|
|
|
const image = canvasAndContext.canvas.toBuffer("image/png");
|
2023-10-14 11:24:56 +02:00
|
|
|
fs.writeFile("output.png", image, function (error) {
|
|
|
|
if (error) {
|
|
|
|
console.error("Error: " + error);
|
|
|
|
} else {
|
|
|
|
console.log("Finished converting first page of PDF file to a PNG image.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Release page resources.
|
|
|
|
page.cleanup();
|
|
|
|
} catch (reason) {
|
|
|
|
console.log(reason);
|
|
|
|
}
|