gpt4 book ai didi

javascript - 使用pdf.js嵌入PDF到网页

转载 作者:行者123 更新时间:2023-11-29 15:40:35 25 4
gpt4 key购买 nike

pdf.js 对于像我这样的新手来说是一个有点大的项目。正如大多数帖子所说,这个项目是将 PDF 文件嵌入到网络中的好工具。但是我很难弄清楚如何使用它。

我想知道的是如何使用pdf.js嵌入本地PDF文件?

最佳答案

您可以提供相对 URL,例如 pdfPath="pdf/TestDocument.pdf"

以下是根据 Github 上提供的示例创建的示例代码:

PDFJS.workerSrc ='PATH_TO_WORKER_LOADER/worker_loader.js';
pdfDoc = PDFJS.getDocument(pdfPath);
pdfDoc.then(renderPdf);

function renderPdf(pdfDoc) {

pdfNumPages = pdfDoc.numPages;

pdfDoc.getPage(1).then(renderPage);
}

function renderPage(page) {

var viewport = page.getViewport(scale);
var $canvas = jQuery("<canvas></canvas>");
//Set the canvas height and width to the height and width of the viewport
var canvas = $canvas.get(0);
var context = canvas.getContext("2d");
canvas.height = viewport.height;
canvas.width = viewport.width;

//Append the canvas to the pdf container div (refer a div in your HTML file)
var $editorTextArea = jQuery("#editorTextArea");
$editorTextArea.css("height", canvas.height + "px").css("width", canvas.width + "px");
$editorTextArea.append($canvas);

var canvasOffset = $canvas.offset();
var $textLayerDiv = jQuery("<div />")
.addClass("textLayer")
.css("height", viewport.height + "px")
.css("width", viewport.width + "px")
.offset({
top: canvasOffset.top,
left: canvasOffset.left
});

//The following few lines of code set up scaling on the context if we are on a HiDPI display
var outputScale = getOutputScale(context);
if (outputScale.scaled) {
var cssScale = 'scale(' + (1 / outputScale.sx) + ', ' +
(1 / outputScale.sy) + ')';
CustomStyle.setProp('transform', canvas, cssScale);
CustomStyle.setProp('transformOrigin', canvas, '0% 0%');
}

context._scaleX = outputScale.sx;
context._scaleY = outputScale.sy;
if (outputScale.scaled) {
context.scale(outputScale.sx, outputScale.sy);
}
var renderContext = {
canvasContext: context,
viewport: viewport,
textLayer: textLayer
};
page.render(renderContext);
});
}

关于javascript - 使用pdf.js嵌入PDF到网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19737905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com