How to display an image in two pages in PDF using jsPDF?

前端 未结 6 1324
庸人自扰
庸人自扰 2020-12-14 05:03

I have a html page. In which I have a button , whenerever I click this button it will convert the entire html page into data image using html2canvas and placed it into PDF u

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 05:25

    To put a long image on multiple pages I came out with something like this:

    var img = new Image();
    img.onload = function(){
        while (croppingYPosition < image.height) {
            var sourceX = 0;
            var sourceY = croppingYPosition;
            var sourceWidth = image.width;
            var sourceHeight = maxHeight;
            var leftToCropHeight = image.height - croppingYPosition;
            if (leftToCropHeight < maxHeight) {
                sourceHeight = leftToCropHeight;
            }
            var destWidth = sourceWidth;
            var destHeight = sourceHeight;
            var destX = 0;
            var destY = 0;
            var canvas = document.createElement('canvas');
            canvas.setAttribute('height', destHeight);
            canvas.setAttribute('width', destWidth);
            var ctx = canvas.getContext('2d');
            ctx.drawImage(img, 
                            sourceX,
                            sourceY,
                            sourceWidth,
                            sourceHeight,
                            destX,
                            destY,
                            destWidth,
                            destHeight);
            croppedImages.push(CanvasToJPEGConversionService.toJPEG(canvas));
            croppingYPosition += destHeight;
        }
        retur croppedImages;
    };
    img.src = image.dataURL;
    

    I basically get an array of objects with dataURL (cropped images).

提交回复
热议问题