2017-06-14 131 views
1

PDFファイルからPDF.jsを使用してサムネイルを生成したいのですが、ファイルが1つしかなく、jsをプロジェクトは次のように記述します:PDF.jsを使用してpdfのサムネイルを生成

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

プロジェクトでPDF.jsを使用するにはどうすればよいですか?バックエンドでPHPを使用しています。 helloworld exampleに基づいて

+0

あなたは、このソリューションを試してみましたか? https://stackoverflow.com/questions/467793/how-do-i-convert-a-pdf-document-to-a-preview-image-in-php –

答えて

1

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; 
 
    }); 
 
} 
 

 
PDFJS.getDocument("//cdn.mozilla.net/pdfjs/tracemonkey.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>

+0

ありがとうございましたが、別のURLで試してみました( http://www.shellgreenier.com/MGreenier-Resume.pdf)この問題が発生しました:要求されたリソースに 'Access-Control-Allow-Origin'ヘッダーがありません。したがって、 'http:// localhost'はアクセスが許可されていません。 –

+0

cdn.mozilla.netにCORSが有効になっています - 異なるWebサイトのデータを提供するためにランダムURLを尋ねることはできません。スクリプトを再生するには、WebサイトでPDFとコードをダウンロードしてください。 https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-xhrも参照してください。 – async5

関連する問題