我开始使用pdf.js库构建pdf查看器。我真的很喜欢一些例子有多简单,所以我使用了PREV / NExt示例来启动我的观众:
https://github.com/mozilla/pdf.js/blob/master/examples/learning/prevnext.html
我想添加放大和缩小功能,并找到了这个简单的查看器,我想对其进行建模并滚动显示:
https://github.com/zedr/simple-pdf-reader.js/blob/master/viewer.js
这是我的index.html的html:
<div class="row">
<div class="col-md-4">
<button class="btn btn-primary" id="prev"><i class="fa fa-level-up fa-lg"></i></button>
<button class="btn btn-primary" id="next"><i class="fa fa-level-down fa-lg"></i></button>
</div><div class="col-md-4">
<span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
<div id="pdf-controls">
<button id="zoom_minus" onclick="url.zoomMinus()"
oncontextmenu="return false;" class="btn btn-primary">-</button>
<button id="zoom_plus" onclick="url.zoomPlus()"
oncontextmenu="return false;" class="btn btn-primary">+</button>
<div id="pdf-stats">
<p>
<span id="pdf-page-zoom">n/a</span> <span>%</span>
</p>
</div>
</div>
</div><div class="col-md-4 pull-right">
<a href="sample.pdf" class="btn btn-primary"><i class="fa fa-arrows-alt fa-lg"></i></a>
<a href="sample.pdf" class="btn btn-primary" download><i class="fa fa-cloud-download fa-lg"></i></a>
</div>
</div>
<br><br>
<center>
<div style="overflow: scroll" id="pdfviewer">
<canvas id="pdfcanvas" style="border:1px solid black; width: 100%"></canvas>
</div>
</center>
这是我的viewer.js
的javascript:
<script id="pdfviewer">
//
// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
//
var url = 'sample.pdf';
//
// Disable workers to avoid yet another cross-origin issue (workers need
// the URL of the script to be loaded, and dynamically loading a cross-origin
// script does not work).
//
// PDFJS.disableWorker = true;
//
// In cases when the pdf.worker.js is located at the different folder than the
// pdf.js's one, or the pdf.js is executed via eval(), the workerSrc property
// shall be specified.
//
PDFJS.workerSrc = 'pdf.worker.js';
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 1.5,
canvas = document.getElementById('pdfcanvas'),
ctx = canvas.getContext('2d');
var camera = {
x: 0,
y: 0,
scale: 1,
};
/**
* Get page info from document, resize canvas accordingly, and render page.
* @param num Page number.
*/
function renderPage(num) {
pageRendering = true;
// 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
};
var renderTask = page.render(renderContext);
// Wait for rendering to finish
renderTask.promise.then(function () {
pageRendering = false;
if (pageNumPending !== null) {
// New page rendering is pending
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
// Update page counters
document.getElementById('page_num').textContent = pageNum;
}
/**
* If another page rendering in progress, waits until the rendering is
* finised. Otherwise, executes rendering immediately.
*/
function queueRenderPage(num) {
if (pageRendering) {
pageNumPending = num;
} else {
renderPage(num);
}
}
/**
* Displays previous page.
*/
function onPrevPage() {
if (pageNum <= 1) {
return;
}
pageNum--;
queueRenderPage(pageNum);
}
document.getElementById('prev').addEventListener('click', onPrevPage);
/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
return;
}
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);
/**
* Asynchronously downloads PDF.
*/
PDFJS.getDocument(url).then(function (pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
//The PdfRead object is a browser-aware reading device that the User will
//manipulate to read the page. Basically, a wrapper around the PdfView object.
var frame = document.getElementById('pdfcanvas');
var zoom_widget = document.getElementById('pdf-page-zoom');
// Keep track of certain values inside the most interesting nodes of the DOM
var state = {
get ctop () { return frame.lastChild.offsetTop },
get ftop () { return frame.scrollTop },
get fsh () { return frame.scrollHeight },
get fh () { return frame.offsetHeight },
};
// Decrease the Zoom, acting on the scale
this.zoomMinus = function (val) {
doc.page.scale -= (val) ? val : 0.25;
zoom_widget.innerText = doc.page.scale * 100;
};
// Increase the Zoom, acting on the scale
this.zoomPlus = function (val) {
doc.page.scale += (val) ? val : 0.25;
zoom_widget.innerText = doc.page.scale * 100;
};
// Controller: monitor for frame scroll events and advance page rendering
frame.onscroll = function () {
var test = (state.fsh - (state.fh + state.ftop));
if (test < 0 && doc.page.head < doc.page.last) {
doc.page.number++;
}
};
// Init the widgets
zoom_widget.innerText = doc.page.scale * 100;
</script>
我尝试将两者集成并向我的观众添加缩放,但没有取得任何成功。与pdf.js的复杂性相比,我的javascript知识非常有限,但我想知道是否有人可以帮我解决我的问题。任何建议,方向,代码将不胜感激。
答案 0 :(得分:14)
抱歉,我知道这是一个老问题,你现在可能想出一个解决方案。但是,您可以在下面编写一个非常简单的PDF.js页面(我通过修改Mozilla网页上的sample进行编码),并使用放大和缩小按钮。
e.range.setNote(cell.getValue() + " --- " + startDate);
抱歉,我还没有检查过上面的代码,看看它与我的代码有什么不同,或者确定它不起作用,但也许这样可以满足您的需求。
答案 1 :(得分:1)
我想添加放大和缩小...
@frabjous答案很好,它可以提供您所描述的内容。现场演示,我也在修补它:http://next.plnkr.co/edit/XQBGOhdzkX3zQ86Y
有关更高级的内容,请检查以下一项:mozilla pdf.js without fullview
使用iframe并将URL作为参数传递似乎很简单!
答案 2 :(得分:0)
可以设置具有任意大小的pdf缩放区域(viewport.transform)的画布(任何大小)的功能
/*
* Load pdf page to canvas and zoom it on arbitrary region
*
* @url - url to pdf
* @pageNum - which page (start from 1)
* @width - canvas width
* @height - canvas height
* @zoom - zoom, 1=no zoom
* @posX - shift pdf on x axis (in pixels)
* @posY - shift pdf on y axis (in pixels)
*/
async function load(url,pageNum,width,height,zoom,posX,posY) {
let pdfjs = window['pdfjs-dist/build/pdf'];
pdfjs.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
let pdf = await pdfjs.getDocument(url).promise;
let page = await pdf.getPage(pageNum);
let viewport = page.getViewport({scale:1});
let pY = viewport.transform[5]*zoom + posY;
viewport.transform = [zoom, 0, 0, -zoom, posX, pY];
let context = canvasPdf.getContext('2d');
canvasPdf.width = width;
canvasPdf.height = height;
let renderContext = {canvasContext: context, viewport};
var render = await page.render(renderContext).promise;
}
let url = "http://cors-anywhere.herokuapp.com/http://www.africau.edu/images/default/sample.pdf";
load(url,1,500,140,2,-140,-110);
body {background: gray }
<script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
<canvas id="canvasPdf"></canvas>