dongye9182
dongye9182
2019-03-28 19:55

使用TCPDF发出保存PDF文件的问题

已采纳

I want to save some html to pdf file on a button click. The problem is that I get the issue after the ajax call:

%PDF-1.7 %���� 8 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20190328213806+02'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 9 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 6 0 R 7 0 R ] /PZ 1 >> endobj 9 0 obj <> stream x��ۏ�q��� |�`ɐVU}���e).e�rI66�$^$�d3� �������]EYb�"I��g?ΙKwU���|�gy�s|r��o�߿������G���vqy|�]��H�������ȄQ��F�D�S��F���@�.��'�}���e�8 �B�@����UH��@M�MC�g���P8���g[t���?P�#E���=��@�P��g� KysL�m�.�0qC�{�[�}Tlg�kY�?���������3�K��9��g�%Mq��=���� 0vw��G�}����Obڃ�l�/��WC��|����?��O���y��u;�ݽ����L �_���qŁ�JG�ׇ��~���a?K�^���g��--�,�L3����?-�Z�e��W!1�D���a�I#���s?

I have found the similar issue here - Tcpdf on Laravel 3 controller

But I don't know what the Response::make is? Any ideas how to fix it? Thanks.

Code:

jQuery/Ajax

$(document).on("click", "#testButtonClick", function() {
   savePDF($("#test").html(), "Test1", "Test2");
 });

function savePDF(info, name, data) {
$.ajax({
    type: "post",
    url: "includes/worker",
    data: {"test": info, "name": name, "data": data},
    xhr: function() {
        try {
            if (window.ActiveXObject) {
                return new window.ActiveXObject("Microsoft.XMLHTTP");
            }
        } catch(e) { }

        return new window.XMLHttpRequest();
    },
    success: function(result) {
        console.log(result);
    },
    error: function(response) {
    }
}); 
}

PHP

    require_once("tcpdf.php");
    $pdfData = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, "UTF-8", false);  //
    $pdfData->SetCreator("Test..");  
    $pdfData->SetTitle("Test..");  
    $pdfData->SetHeaderData("", "", PDF_HEADER_TITLE, PDF_HEADER_STRING);  
    $pdfData->setHeaderFont(Array(PDF_FONT_NAME_MAIN, "", PDF_FONT_SIZE_MAIN));  
    $pdfData->setFooterFont(Array(PDF_FONT_NAME_DATA, "", PDF_FONT_SIZE_DATA));  
    $pdfData->SetFooterMargin(PDF_MARGIN_FOOTER);  
    $pdfData->SetMargins(PDF_MARGIN_LEFT, "5", PDF_MARGIN_RIGHT);  
    $pdfData->setPrintHeader(false);  
    $pdfData->setPrintFooter(false);  
    $pdfData->SetAutoPageBreak(true, 10);  
    $pdfData->setFontSubsetting(true);
    $pdfData->AddPage();
    $pdfData->SetFont("freeserif", "", 10);
    $pdfData->writeHTML(iconv("Windows-1251", "UTF-8", "Это тест...."));  
    $pdfData->Output("Test.pdf", "D");
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • drxyaox153896 drxyaox153896 2年前

    So, I have fixed it by using auto form JS and escaping characters such as ".

    Code:

        var pdfForm = $("<form action=\"includes/worker\" method=\"post\" enctype=\"multipart/form-data\" target=\"_blank\"></form>");
          pdfForm.append("<input type=\"hidden\" name=\"test\" value=\"" + htmlValidation($("#test").html()) + "\">");
          pdfForm.append("<input type=\"hidden\" name=\"name\" value=\"Test1\">");
          pdfForm.append("<input type=\"hidden\" name=\"data\" value=\"Test2\">");
          $("body").append(pdfForm);
          $(pdfForm).submit();
    
       function htmlValidation(data) {
         return data
           .replace(/&/g, "&amp;")
           .replace(/</g, "&lt;")
           .replace(/>/g, "&gt;")
           .replace(/"/g, "&quot;")
           .replace(/'/g, "&#039;");
       }
    
    点赞 评论 复制链接分享

相关推荐