首页 > 解决方案 > 如何使用 php 函数从 jquery 显示 pdf 文档

问题描述

我目前正在修改 wordpress 主题的功能,它使用 jQuery 和 php 在新选项卡中编写 html。我想用 pdf 文档替换 html,但我不能让它工作。

我使用TCPDF生成 pdf 文件。当我尝试从不涉及任何 jQuery 的单独 php 文件生成此 pdf 时,它可以完美运行。

如果我将所有的 TCPDF 代码放在一个单独的 php 文件中并从 shell 运行它php pdftest.php,输出与我在网页中得到的输出相同。

结果我得到

我想得到的结果

我试着让它成为一个 GET 请求,在同一个窗口中写入 pdf,但这没有用......

这是jQuery代码

if( $('.houzez-print').length > 0 ) {
        $('.houzez-print').on('click', function (e) {
            e.preventDefault();
            var propID, printWindow;

            propID = $(this).attr('data-propid');

            printWindow = window.open('', 'Print Me', 'width=800 ,height=842');
            $.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {
                    'action': 'houzez_create_print',
                    'propid': propID,
                },
                success: function (data) {
                    printWindow.document.write(data);
                    printWindow.document.close();
                    printWindow.focus();
                },
                error: function (xhr, status, error) {
                    var err = eval("(" + xhr.responseText + ")");
                    console.log(err.Message);
                }
            });
        });
    }

PHP 代码(默认 TCPDF 示例)

function houzez_create_print () {
        if(!isset($_POST['propid'])|| !is_numeric($_POST['propid'])){
            exit();
        }
        global $hide_fields;
        $hide_fields = houzez_option('hide_detail_prop_fields');
        $property_id = intval($_POST['propid']);

        require_once ABSPATH . WPINC . '/TCPDF-6.4.1/tcpdf.php';

        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('TEST');
        $pdf->SetTitle('test document');

        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
        $pdf->setFooterData(array(0,64,0), array(0,64,128));

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // ---------------------------------------------------------

        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage('L', 'A3');


        // Set some content to print
        $html = <<<EOD
        <h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
        <i>This is the first example of TCPDF library.</i>
        <p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
        <p>Please check the source code documentation and other examples for further information.</p>
        <p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
        EOD;

        // Print text using writeHTMLCell()
        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

        // ---------------------------------------------------------
        while (ob_get_level())
            ob_end_clean();

        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('maison_a3.pdf', 'I');
}

标签: javascriptphpjquerypdfwordpress-theming

解决方案


推荐阅读