【发布时间】:2018-01-13 22:09:06
【问题描述】:
我正在尝试在我的项目中实现 PDF.js,但比预期的要难。
目前,我可以在 div 中呈现整个 PDF,但无法呈现标准工具栏查看器;您可以在此演示页面顶部看到我想要的示例:https://mozilla.github.io/pdf.js/web/viewer.html
我的 JS 代码现在是这样的:
var url = '/filemanager/example.pdf';
var pdfScale = 1;
PDFJS.workerSrc = '/js/pdfJs/build/pdf.worker.js';
function renderPDF(url, canvasContainer, options) {
var options = options || { scale: pdfScale };
function renderPage(page) {
var viewport = page.getViewport(options.scale);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
canvas.height = viewport.height;
canvas.width = viewport.width;
canvasContainer.appendChild(canvas);
page.render(renderContext);
}
function renderPages(pdfDoc) {
for(var num = 1; num <= pdfDoc.numPages; num++)
pdfDoc.getPage(num).then(renderPage);
}
PDFJS.disableWorker = true;
PDFJS.getDocument(url).then(renderPages);
}
renderPDF(url, document.getElementById('the-canvas'));
<div id="the-canvas" class="text-center" style="overflow: auto; max-height: 450px"></div>
那么...我怎样才能使用演示页面等所有控件的工具栏查看器?
【问题讨论】:
-
不,我该如何应用它?你能发布一个代码示例吗?
-
@GabrielChiHongLee 我知道,但我不知道如何在我的项目中使用它。你能给我什么建议吗?
-
@IsaacBosca 您可以只引用 view.html 文件属性是您的 pdf 文件的位置,即
http://www.example.com/viewer.html?file=http://www.example.com/file.pdfper 此处的文档,github.com/mozilla/pdf.js/wiki/Setup-PDF.js-in-a-website
标签: javascript html pdf pdf.js pdfjs-dist