0

I am trying to convert html into a pdf client side using jspdf. The code I have currently is this: (most of this is for pagebreaks

    $(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 imageData = canvas.toDataURL("image/jpeg");
                var image = new Image();
                image = Canvas2Image.convertToJPEG(canvas);
                var doc = new jsPDF();
                doc.page=1;
                //FIRST PAGE****************************************
                doc.addImage(imageData, 'JPEG', -115, 5, 440 , 875);

                var croppingYPosition = 1250;
                count = (image.height) / 3300;
                for (var i =1; !(i >= count); i++) {
                    doc.addPage();
                    var sourceX = 0;
                    var sourceY = croppingYPosition;
                    var sourceWidth = image.width;
                    var sourceHeight = 1100;
                    var destWidth = 1600;
                    var destHeight = 1090;
                    var destX = -450
                    var destY = 0;
                    var canvas1 = document.createElement('canvas');
                    canvas1.setAttribute('height', 1200);
                    canvas1.setAttribute('width', destWidth);
                    var ctx = canvas1.getContext("2d");
                    ctx.drawImage(image,
                                    sourceX,
                                    sourceY,
                                    sourceWidth,
                                    sourceHeight,
                                    destX,
                                    destY,
                                    destWidth,
                                    destHeight);
                    var image2 = new Image();
                    image2 = Canvas2Image.convertToJPEG(canvas1);
                    image2Data = image2.src;
                    doc.addImage(image2Data, 'JPEG', 12, 10);
                    croppingYPosition += 1230;
                }

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

However, I would like to, rather than using arbitrary, hard-coded pixel values for setting pagebreaks, have it search for a specific html tag, id, class, or some other identifier to search for at which to make a page break. For example, The code looks down the html until it finds something of class .pagebreak at which point it takes everything above it and stretches it to fit a pdf page (I can do the stretchy part, I just need help with the actual page break) and then looks right after the page break and starts over. Hope this made sense. Thanks!

yodofizzy
  • 63
  • 2
  • 14
  • 1
    Did you see [this comment](https://github.com/MrRio/jsPDF/issues/46) that says you can insert an HTML comment of ``? I haven't verified that it works but I can see that its still in the source code. – Chris Haas Jun 19 '14 at 15:50
  • Wow that looks convenient, and thanks for the help, but I'm not exactly sure how I would actually implement that. (sorry, noob here) – yodofizzy Jun 19 '14 at 17:25
  • Do you control the HTML? – Chris Haas Jun 19 '14 at 19:29
  • I think so? Do you mean am I editing it? If so, yes – yodofizzy Jun 20 '14 at 13:37
  • Then wherever you want to break to occur you should be able to add that block off code – Chris Haas Jun 20 '14 at 13:39
  • And then what should I go about doing with the rest of my javascript? Thank you so much for your help – yodofizzy Jun 20 '14 at 14:23
  • According to that comment above, if you put that in your HTML you shouldn't have to change your JS in any way, page breaks should happen whenever the library encounters that block of code. – Chris Haas Jun 20 '14 at 14:51
  • Well bummer. It's not working for me. Though I'm sure I'm doing something wrong. oh well. Thanks for all of your help. I do have an altered version of this code working in chrome, but alas, it doesn't seem to work well with firefox or IE – yodofizzy Jun 20 '14 at 15:48

0 Answers0