Publish: 2018-02-08 | Modify: 2021-12-17
In the case of simply previewing PDF files with low UI requirements, you can use the href
attribute of the <a>
tag to achieve the preview.
<a href="document_url"></a>
This plugin can achieve PDF preview (including other media files), but it is not able to handle Word and other file types. Here is the implementation:
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery.media.js"></script>
<body>
<div id="handout_wrap_inner"></div>
</body>
<script type="text/javascript">
$('#handout_wrap_inner').media({
width: '100%',
height: '100%',
autoplay: true,
src: 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf',
});
</script>
<iframe src="previewUrl" width="100%" height="362px" frameborder="1"></iframe>
<embed src="previewUrl" type="application/pdf" width="100%" height="100%">
<object src="previewUrl" width="100%" height="100%">
This browser does not support PDFs. Please download the PDF to view it: <a href="previewUrl">Download PDF</a>
</object>
<!DOCTYPE html>
<html>
<head>
<title>Show PDF</title>
<meta charset="utf-8" />
<script type="text/javascript" src="pdfobject.min.js"></script>
<style type="text/css">
html,body,#pdf_viewer{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="pdf_viewer"></div>
</body>
<script type="text/javascript">
if(PDFObject.supportsPDFs){
PDFObject.embed("index.pdf", "#pdf_viewer" );
} else {
location.href = "/canvas";
}
</script>
</html>
PDF.js is an open-source PDF document reading and parsing plugin that can render PDF files as Canvas. It consists of two library files: pdf.js and pdf.worker.js.
To achieve online preview for Word, PowerPoint, and Excel files, you can use the Microsoft online preview function (the resource must be publicly accessible).
<iframe src="https://view.officeapps.live.com/op/view.aspx?src=http://storage.xuetangx.com/public_assets/xuetangx/PDF/1.xls" width="100%" height="100%" frameborder="1"></iframe>
You can use XDOC to preview DOC documents represented by DataURI. XDOC can also be used to preview text, parameterized text, HTML text, JSON text, official documents, etc.
You can use sheet.js to parse Excel files and render them as images.
Summary:
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.