yodofizzy
yodofizzy

Reputation: 83

Generating pdfs with jsPDF compatibility issues in Firefox?

sorry for the inundation of PDF generation questions. I have been working on a project that converts a page into a multipage pdf(with jsPDF) using pagebreaks. I (finally!) got the project working in google Chrome but upon going to test it in Firefox, I was able to generate the first page, but the second two pages showed up entirely black. Here is the code:

$(document).ready(function() 
{
    $("#runpdf").click(function(event) 
    {
        var partsec = $("main_body page1");
        html2canvas(document.body,
        {
            logging: true,
            profile: true,
            allowTaint: true,
            letterRendering: true,
            onrendered: function(canvas) 
            {
                var sectionHeight = $("section").height();
                var sectionWidth = $("#width").width();

                var doc = new jsPDF();
                var image = new Image();
                var imageData = canvas.toDataURL("image/jpeg");
                image = Canvas2Image.convertToJPEG(canvas);
                doc.addImage(imageData,'JPEG', -115, 5, 440, 875);


                doc.addPage();

                var canvas1 = document.createElement('canvas');
                canvas1.setAttribute('height', sectionHeight);
                canvas1.setAttribute('width', sectionWidth);
                var ctx = canvas1.getContext("2d");
                ctx.drawImage(image, 0, 1025, sectionWidth, 1250, 0, 0, 1800, 950);
                var image2 = new Image();
                image2 = Canvas2Image.convertToJPEG(canvas1);
                image2Data = image2.src;
                doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);


                doc.addPage();                  
                var canvas2 = document.createElement('canvas');
                canvas2.setAttribute('height', sectionHeight);
                canvas2.setAttribute('width', sectionWidth);
                var ctx1 = canvas2.getContext("2d");
                ctx1.drawImage(image, 0, 2050, sectionWidth, 1250, 0, 0, 1800, 1000);
                var image3 = new Image();
                image3 = Canvas2Image.convertToJPEG(canvas2);
                image2Data = image3.src;
                doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);




                doc.save('test.pdf');
            }
        });
    });
});         

As you can see, each page is generated separately, and since the first page is functioning, but not the second two, I am assuming that the problem is with the getContext, or drawImage functions. How could I change this or add something that will enable this to work properly in Firefox. Thanks again.

Upvotes: 0

Views: 2377

Answers (1)

devesh
devesh

Reputation: 106

Instead of directly using the doc.save, you can try to save the blob into the pdf, here is the modified code

$(document).ready(function() 
{
    $("#runpdf").click(function(event) 
    {
        var partsec = $("main_body page1");
        html2canvas(document.body,
        {
            logging: true,
            profile: true,
            allowTaint: true,
            letterRendering: true,
            onrendered: function(canvas) 
            {
                var sectionHeight = $("section").height();
                var sectionWidth = $("#width").width();

                var doc = new jsPDF();
                var image = new Image();
                var imageData = canvas.toDataURL("image/jpeg");
                image = Canvas2Image.convertToJPEG(canvas);
                doc.addImage(imageData,'JPEG', -115, 5, 440, 875);


                doc.addPage();

                var canvas1 = document.createElement('canvas');
                canvas1.setAttribute('height', sectionHeight);
                canvas1.setAttribute('width', sectionWidth);
                var ctx = canvas1.getContext("2d");
                ctx.drawImage(image, 0, 1025, sectionWidth, 1250, 0, 0, 1800, 950);
                var image2 = new Image();
                image2 = Canvas2Image.convertToJPEG(canvas1);
                image2Data = image2.src;
                doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);


                doc.addPage();                  
                var canvas2 = document.createElement('canvas');
                canvas2.setAttribute('height', sectionHeight);
                canvas2.setAttribute('width', sectionWidth);
                var ctx1 = canvas2.getContext("2d");
                ctx1.drawImage(image, 0, 2050, sectionWidth, 1250, 0, 0, 1800, 1000);
                var image3 = new Image();
                image3 = Canvas2Image.convertToJPEG(canvas2);
                image2Data = image3.src;
                doc.addImage(image2Data, 'JPEG', -105, 5, 440, 325);

                var data = doc.output();
                var buffer = new ArrayBuffer(data.length);
                var array = new Uint8Array(buffer);
                for (var i = 0; i < data.length; i++) {
                  array[i] = data.charCodeAt(i);
                }

                var blob = new Blob(
                  [array],
                  {type: 'application/pdf', encoding: 'raw'}
                );
                saveAs(blob, "test.pdf");

            });
    });
});

The javascript being used here apart from jspdf are canvas-toBlob.js and FileSaver.js

Upvotes: 1

Related Questions