Display PDF on Android Web Browser | No Download Required
If you want to display a PDF on an Android web browser without requiring the user to download it, there are a few options available.
One option is to use Google Drive. First, upload the PDF to your Google Drive account. Then, create a shareable link for the file. Finally, use the link to embed the PDF in your website using the <iframe>
tag.
Another option is to use a PDF.js library. This library allows you to render PDF files in a web browser without requiring any plugins or downloads. You can use the library by including the <script>
tag in your HTML code.
Here's an example of code you can use to display a PDF on an Android web browser without requiring a download:
<iframe src="https://drive.google.com/file/d/your-pdf-file-id-goes-here/preview" width="100%" height="500px"></iframe>
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
<script>
// Fetch the PDF document from the URL using promises
pdfjsLib.getDocument('your-pdf-file-url-goes-here').promise.then(function(pdf) {
// Render the PDF pages using promises
var viewer = document.getElementById('pdf-viewer');
for(var pageNumber=1; pageNumber <= pdf.numPages; pageNumber++) {
pdf.getPage(pageNumber).then(function(page) {
var scale = 1.5;
var viewport = page.getViewport({ scale: scale });
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
viewer.appendChild(canvas);
page.render({ canvasContext: context, viewport: viewport });
});
}
});
</script>
By using one of these options, you can display a PDF on an Android web browser without requiring the user to download it.
Leave a Reply