I would like to improve my script in order to set PDF cover page for each object when user sets the mouve over the object on the template.
Actual process:
Up to now, I can upload .pdf
and .epub
file formats according to an object with some additionnals fields : title
, language
...
The PDF is stored into media
directory in my project.
In the main page, I display all objects like this :
As you can see in the end of each publication, there is a glyphicon
which let, up to now, to display inside another tab the object PDF file.
Expected idea:
I would like to know if there is a way to display a window with the PDF file. The better thing will be to display only the first PDF page in order to define a cover page for each object.
In my HTML file, I have this line which let to display my PDF into another tab :
<td class="col-md-1"><a id="download-image_{{ document.id }}" href="http://localhost:8000/media/{{ document.upload }}"><span class="glyphicon glyphicon-blackboard"></span></a></td>
I assume I need to handle this line and add javascript from pdf.js
in order to get only the first page and display it as a picture.
I read the PDF.js documentation and I would like to get some help in order to display my first PDF page. Documentation and examples from PDF.js are miscellaneous but I don't find how I can do that.
Edit :
Thanks to Kedas mendi, this is what I have according to his answer. But I don't overcome to display my pdf.
In my html file I have a table with :
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
<script src="../static/freepub/js/customPdf.js"></script>
<table>
<td class="col-md-1">
<canvas class="the-canvas"><a href="http://localhost:8000/media/{{ document.upload }}"><span class="glyphicon glyphicon-blackboard"></span></a></canvas>
</td>
</table>
And my customPdf.js file :
pdfjsLib.getDocument('http://localhost:8000/media/media/loremipsum.pdf').then(function(pdf) {
pdf.getPage(1).then(function(page) {
var scale = 1.5;
var viewport = page.getViewport(scale);
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
});
page.render(renderContext);
});
Up to now, getDocument()
is pointing to a specific pdf file, but it will be a django variable at the end.
Anyway, I don"t display any PDF file