<html>
<body>
<div>
<button id="prev" onclick="goPrevious()">Previous</button>
<button id="next" onclick="goNext()">Next</button>
<span>Page: <span id="page_numo"></span> / <span id="page_count"></span></span>
<input type=text id="elNum" value=1>
</div>
<div>
<canvas id="the-canvas" style="border:0px solid black"></canvas>
</div>
<!-- Use latest PDF.js build from Github -->
<script type="text/javascript" src="https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>
<script type="text/javascript">
//
// NOTE:
// Modifying the URL below to another server will likely *NOT* work. Because of browser
// security restrictions, we have to use a file server with special headers
// (CORS) - most servers don't support cross-origin browser requests.
//
var url = 'http://cdn.mozilla.net/pdfjs/tracemonkey.pdf';
//
// Disable workers to avoid yet another cross-origin issue (workers need the URL of
// the script to be loaded, and currently do not allow cross-origin scripts)
//
PDFJS.disableWorker = true;
var pdfDoc = null,
pageNum = 1,
scale = 0.8,
canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
//
// Get page info from document, resize canvas accordingly, and render page
//
function renderPage(num) {
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
page.render(renderContext);
});
// Update page counters
document.getElementById('page_numo').textContent = pageNum;
document.getElementById('page_count').textContent = pdfDoc.numPages;
sic = document.getElementById('elNum');
sic.value = pageNum;
}
crc = 1;
//
// Go to previous page
//
function goPrevious() {
if (pageNum <= 1)
return;
pageNum--;
crc = pageNum;
renderPage(pageNum);
}
//
// Go to next page
//
function goNext() {
if (pageNum >= pdfDoc.numPages)
return;
pageNum++;
crc = pageNum;
renderPage(pageNum);
}
//
// Asynchronously download PDF as an ArrayBuffer
//
PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
pdfDoc = _pdfDoc;
renderPage(pageNum);
});
document.write ('Current page is '+crc);
</script>
</body>
</html>
それがコードです。これは、オンライン pdf レンダラー (pdf.js) の最も単純なバージョンです。私は、pageNum (現在の pdf ページ番号) 変数を URL 経由でプロセス用の別のスクリプトに渡す必要があるプロジェクトに取り組んでいます。しかし、問題は、renderPage 関数の外で pageNum 値を取得できなかったことです。Javascript 変数スコープの問題だと思いますが、crc 変数または document.getElementById('elNum').value を介して抽出しようとすると何が問題なのかわかりません... (document.write ('Current page is '+ crc); スクリプトの最後の行と 8 行目のフィールドを参照して、どのように値を取得しようとしているかを確認してください)。
私が間違っていることについて、誰かが私に手がかりを与えることができますか? テストページはこちら ( http://jsbin.com/pdfjs-prevnext-v2/2091/edit )
注: このトピックに関する他の質問を見たことがありますが、これは奇妙なケースのようです。本当に助けが必要で、うまくいけば他の誰かが将来それを必要とするでしょう.