簡體   English   中英

使用 PDF.js 生成 pdf 的縮略圖

[英]Generating thumbnail of a pdf using PDF.js

我想使用 PDF.js 從 pdf 文件生成縮略圖,但它不像只有一個文件的其他 js 並且在您的項目中包含 js 所需的全部內容是編寫:

<script src="any.js"></script>

如何在我的項目中使用 PDF.js? 我在后端使用 PHP。

基於helloworld 示例

 function makeThumb(page) { // draw page to fit into 96x96 canvas var vp = page.getViewport(1); var canvas = document.createElement("canvas"); canvas.width = canvas.height = 96; var scale = Math.min(canvas.width / vp.width, canvas.height / vp.height); return page.render({canvasContext: canvas.getContext("2d"), viewport: page.getViewport(scale)}).promise.then(function () { return canvas; }); } pdfjsLib.getDocument("https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf").promise.then(function (doc) { var pages = []; while (pages.length < doc.numPages) pages.push(pages.length + 1); return Promise.all(pages.map(function (num) { // create a div for each page and build a small canvas for it var div = document.createElement("div"); document.body.appendChild(div); return doc.getPage(num).then(makeThumb) .then(function (canvas) { div.appendChild(canvas); }); })); }).catch(console.error);
 <script src="//npmcdn.com/pdfjs-dist/build/pdf.js"></script>

我想通了,比例不是參數。 參數是一個需要設置的具有規模字段的對象。

function makeThumb(page) {
    // draw page to fit into 96x96 canvas
    var vp = page.getViewport({ scale: 1, });
    var canvas = document.createElement("canvas");
    var scalesize = 1;
    canvas.width = vp.width * scalesize;
    canvas.height = vp.height * scalesize;
    var scale = Math.min(canvas.width / vp.width, canvas.height / vp.height);
    console.log(vp.width, vp.height, scale);
    return page.render({ canvasContext: canvas.getContext("2d"), viewport: page.getViewport({ scale: scale }) }).promise.then(function () {
        return canvas; 
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM