dongye9182 2019-03-28 19:55
浏览 483
已采纳

使用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 2019-03-29 12:45
    关注

    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;");
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料