2014-04-10 11:59:19 +09:00
|
|
|
/* Copyright 2014 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.
|
|
|
|
*/
|
|
|
|
|
2021-03-13 01:08:17 +09:00
|
|
|
const PDF_PATH = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
|
|
|
const PAGE_NUMBER = 1;
|
|
|
|
const PAGE_SCALE = 1.5;
|
|
|
|
const SVG_NS = "http://www.w3.org/2000/svg";
|
2014-04-10 11:59:19 +09:00
|
|
|
|
2018-03-16 05:49:28 +09:00
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
2023-10-14 18:24:56 +09:00
|
|
|
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
2015-11-06 22:39:13 +09:00
|
|
|
|
2014-04-10 11:59:19 +09:00
|
|
|
function buildSVG(viewport, textContent) {
|
|
|
|
// Building SVG with size of the viewport (for simplicity)
|
2021-03-13 01:08:17 +09:00
|
|
|
const svg = document.createElementNS(SVG_NS, "svg:svg");
|
2014-04-10 11:59:19 +09:00
|
|
|
svg.setAttribute("width", viewport.width + "px");
|
|
|
|
svg.setAttribute("height", viewport.height + "px");
|
|
|
|
// items are transformed to have 1px font size
|
|
|
|
svg.setAttribute("font-size", 1);
|
|
|
|
|
|
|
|
// processing all items
|
2020-04-14 19:28:14 +09:00
|
|
|
textContent.items.forEach(function (textItem) {
|
2016-07-17 21:33:41 +09:00
|
|
|
// we have to take in account viewport transform, which includes scale,
|
2014-04-10 11:59:19 +09:00
|
|
|
// rotation and Y-axis flip, and not forgetting to flip text.
|
2021-03-13 01:08:17 +09:00
|
|
|
const tx = pdfjsLib.Util.transform(
|
2018-03-16 05:49:28 +09:00
|
|
|
pdfjsLib.Util.transform(viewport.transform, textItem.transform),
|
2014-04-10 11:59:19 +09:00
|
|
|
[1, 0, 0, -1, 0, 0]
|
|
|
|
);
|
2021-03-13 01:08:17 +09:00
|
|
|
const style = textContent.styles[textItem.fontName];
|
2014-04-10 11:59:19 +09:00
|
|
|
// adding text element
|
2021-03-13 01:08:17 +09:00
|
|
|
const text = document.createElementNS(SVG_NS, "svg:text");
|
2014-04-10 11:59:19 +09:00
|
|
|
text.setAttribute("transform", "matrix(" + tx.join(" ") + ")");
|
|
|
|
text.setAttribute("font-family", style.fontFamily);
|
|
|
|
text.textContent = textItem.str;
|
2022-06-12 19:20:25 +09:00
|
|
|
svg.append(text);
|
2014-04-10 11:59:19 +09:00
|
|
|
});
|
|
|
|
return svg;
|
|
|
|
}
|
|
|
|
|
2021-10-14 22:44:00 +09:00
|
|
|
async function pageLoaded() {
|
2014-04-10 11:59:19 +09:00
|
|
|
// Loading document and page text content
|
2021-03-13 01:08:17 +09:00
|
|
|
const loadingTask = pdfjsLib.getDocument({ url: PDF_PATH });
|
2021-10-14 22:44:00 +09:00
|
|
|
const pdfDocument = await loadingTask.promise;
|
|
|
|
const page = await pdfDocument.getPage(PAGE_NUMBER);
|
|
|
|
const viewport = page.getViewport({ scale: PAGE_SCALE });
|
|
|
|
const textContent = await page.getTextContent();
|
|
|
|
// building SVG and adding that to the DOM
|
|
|
|
const svg = buildSVG(viewport, textContent);
|
2022-06-12 19:20:25 +09:00
|
|
|
document.getElementById("pageContainer").append(svg);
|
2021-11-30 21:11:50 +09:00
|
|
|
// Release page resources.
|
|
|
|
page.cleanup();
|
2014-04-10 11:59:19 +09:00
|
|
|
}
|
|
|
|
|
2020-04-14 19:28:14 +09:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
2018-03-16 05:49:28 +09:00
|
|
|
if (typeof pdfjsLib === "undefined") {
|
2021-01-23 01:38:26 +09:00
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
2014-04-10 11:59:19 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pageLoaded();
|
|
|
|
});
|