函数doc.save width jspdf

时间:2016-04-08 09:15:53

标签: php jspdf

实际上,我们从HTML文件中为每个客户生成管理文档。 另一方面,生成的pdf将保存到服务器上的特定文件夹中,以便发送给相关客户。

我可以通过doc.save命令提供备份路径吗? 我的代码:

function createpdf() {
var canvasToImage = function(canvas){
    var img = new Image();
    var dataURL = canvas.toDataURL('image/png');
    img.src = dataURL;
    return img;
};
var a4  =[595.28,2000];
var canvasShiftImage = function(oldCanvas,shiftAmt){
    shiftAmt = parseInt(shiftAmt) || 0;
    if(!shiftAmt){ return oldCanvas; }

    var newCanvas = document.createElement('canvas');
    newCanvas.height = oldCanvas.height - shiftAmt;
    newCanvas.width = oldCanvas.width;
    var ctx = newCanvas.getContext('2d');

    var img = canvasToImage(oldCanvas);
    ctx.drawImage(img,0, shiftAmt, img.width, img.height, 0, 0, img.width, img.height);

    return newCanvas;
};


var canvasToImageSuccess = function(canvas){
    var pdf = new jsPDF('p','px','letter'),
        pdfInternals = pdf.internal,
        pdfPageSize = pdfInternals.pageSize,
        pdfScaleFactor = pdfInternals.scaleFactor,
        pdfPageWidth = pdfPageSize.width,
        pdfPageHeight = pdfPageSize.height,
        totalPdfHeight = 0,
        htmlPageHeight = canvas.height,
        htmlScaleFactor = canvas.width / (pdfPageWidth * pdfScaleFactor),
        safetyNet = 0;

    while(totalPdfHeight < htmlPageHeight && safetyNet < 15){
        var newCanvas = canvasShiftImage(canvas, totalPdfHeight);
        pdf.addImage(newCanvas, 'png',0, 0, pdfPageWidth, 0, null, 'NONE');

        totalPdfHeight += (pdfPageHeight * pdfScaleFactor * htmlScaleFactor );

        if(totalPdfHeight < htmlPageHeight){
            pdf.addPage();
        }
        safetyNet++;
    }

    pdf.save('<?php echo $mavariable?>.pdf');
};

html2canvas($('main')[0], {
    onrendered: function(canvas){
        canvasToImageSuccess(canvas);
    }
});

}

1 个答案:

答案 0 :(得分:0)

我想你想在邮件附件中发送PDF。对于客户端代码,使用生成的PDF(通过jsPDF)base64字符串格式向服务器发出请求。

var pdfData = pdf.output();

您可以再次将PDF数据(base64)发布到服务器,您已将base64字符串转换为PDF,然后将其保存到任何临时路径(如果需要)。因此,您可以将其邮寄给PDF。

https://stackoverflow.com/a/35684594/2090459