I am unable to load a Konva Image object in my canvas if the source is a PDF.
No errors are thrown but the canvas is blank where the image should be. Are there any known workarounds for this? Any suggestions welcome.
I am unable to load a Konva Image object in my canvas if the source is a PDF.
No errors are thrown but the canvas is blank where the image should be. Are there any known workarounds for this? Any suggestions welcome.
PDF is not an image. It is a very complex document format. You can't use it directly.
Probably you can use something like this https://mozilla.github.io/pdf.js/ to render pdf and then use it somehow.
The HTML5 canvas is hosted in a browser and the browser is responsible for converting images from file data to image data. As a rule of thumb, if you can open an image in your browser, meaning via the html img tag without any special plugins, then it might work with the canvas, but this will be down to the capabilities of the browser.
PDF is not an image format, but a scriptable rich text document format that can contain different types of multimedia content, including vector and bitmap graphics, audio, video, forms.
If you do happen to get this to work, be very careful about cross-device and cross browser testing.
This is not a bug in Konva.
can you try this solution combination between fabricjs and pdf of mozilla
/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
return;
}
prevPageNum = pageNum;
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);
/**
* Asynchronously downloads PDF.
*/
pdfjsLib.getDocument(url).then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
// Adding a rectangle
jQuery(function($) {
$("#addRectangle").click(function() {
fCanvas.add(new fabric.Rect({
left: 100,
top: 100,
fill: 'red',
width: 20,
height: 20
}));
});
});
<script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<h1>PDF.js Previous/Next example</h1>
<div>
<input type="button" id="addRectangle" value="Add Signature">
<button id="prev">Previous</button>
<button id="next">Next</button>
<span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
<canvas id="the-canvas"></canvas>