Skip to content

Instantly share code, notes, and snippets.

@aemarkov
Created May 21, 2016 09:56
Show Gist options
  • Select an option

  • Save aemarkov/6c4d40fa0afc2fa22c4fcaf0ce5fe42f to your computer and use it in GitHub Desktop.

Select an option

Save aemarkov/6c4d40fa0afc2fa22c4fcaf0ce5fe42f to your computer and use it in GitHub Desktop.
# customview.js
// 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.
//
//
// 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 = false;
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 1.0,
numPages = 1;
//canvas = document.getElementById('the-canvas')
show_pdf('1.pdf');
//Передача файла в виде base64 (не используется, и так работает)
function show_pdf_base64(base64)
{
alert('DECODING BASE64...');
data = fromBase64(base64);
alert('DECODED');
PDFJS.getDocument(data).then(getPdf);
}
function fromBase64(dataURI) {
var raw = window.atob(dataURI);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
//Передача файла в виде полного имени (работает)
function show_pdf(url)
{
PDFJS.getDocument(url).then(getPdf);
}
//Рендер пдф
function getPdf(pdfDoc_)
{
pdfDoc = pdfDoc_;
numPages = pdfDoc.numPages;
pageNum=1;
pdfDoc.getPage(pageNum).then(handlePages);
}
//Рендер страницы
function handlePages(page)
{
//create new canvas
var viewport = page.getViewport(1);
var canvas = document.createElement( "canvas" );
canvas.style.display="block";
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
//render page
page.render({canvasContext: context, viewport: viewport});
//add canvas to body
document.body.appendChild(canvas);
//render new page
pageNum++;
if(pdfDoc!=null && pageNum<=numPages)
pdfDoc.getPage(pageNum).then(handlePages);
}
# html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="minimal.css"/>
<script type="text/javascript" src="file:///android_asset/pdfviewer/compatibility.js"></script>
<script type="text/javascript" src="file:///android_asset/pdfviewer/pdf.js"></script>
<script type="text/javascript" src="file:///android_asset/pdfviewer/customview.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
</body>
</html>
#java
webView.setWebChromeClient(new WebChromeClient());
final Uri path = Uri.fromFile(pdf_file);
//Обработчик событий WebView
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
String call = "show_pdf('"+path.toString()+"');";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript(call, null);
}
else
webView.loadUrl("javascript:"+call);
}
});
webView.loadUrl("file:///android_asset/pdfviewer/index.html");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment