如何在已加载的页面上生成和显示TCPDF pdf?

时间:2017-07-21 14:57:53

标签: php html tcpdf

我正在尝试使用TCPDF动态生成PDF并将其显示在浏览器中。

我已经......

  • 输出PDF作为下载
  • 在没有任何HTML的情况下输出PDF内联。 (使用$pdf->Output('example_007.pdf', 'I');

我想要做的是......

  • 使用已打印的HTML内联输出PDF,而不会清除该HTML。

当然我遇到了TCPDF ERROR: Some data has already been output, can't send PDF file问题。我设法使用ob函数来消除此错误,例如ob_startob_flush;但是,这会清除我现有的HTML。

我尝试在通过AJAX(纯JavaScript)调用的PHP文件上输出PDF;但是,我没有任何运气。什么都没有出现过。

那么在内容输出的同时,如何在浏览器中显示PDF?

我或多或少地寻找以下内容:

enter image description here

我目前正在使用测试页面,这是我的代码:...大部分来自TCPDF的示例。

//The following PHP block was tried but also failed as it wiped existing HTML
<?php
    ob_start();
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Test PDF</title>
    </head>
    <body>
        <h1>Before PDF</h1>
        <?php
            error_Reporting(E_ALL);

            date_default_timezone_set("America/New_York");

            // Include the main TCPDF library (search for installation path).
            require_once $_SERVER["DOCUMENT_ROOT"]."/my-site/TCPDF-master/tcpdf.php";

            //ob_start() Tried this at some point... Didn't work

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

            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Nicola Asuni');
            $pdf->SetTitle('TCPDF Example 001');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            // 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 some language-dependent strings (optional)
            if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
                require_once(dirname(__FILE__).'/lang/eng.php');
                $pdf->setLanguageArray($l);
            }

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

            // 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();

            // set text shadow effect
            $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

            // Set some content to print
            $html = "
                <h1>Hello World!</h1>
            ";

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

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

            // Close and output PDF document
            // This method has several options, check the source code documentation for more information.

            //ob_clean(); Various posts suggested various combinations of these methods
            //ob_flush();
            $pdf->Output('example_007.pdf', 'I');
            //ob_end_flush();
            //ob_end_clean();
        ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

使用您用于显示内联的相同技术,您可以尝试使用Iframe加载PDF文件:

<html>
    <head>
    </head>
    <body>
        <p>some content</p>
        <iframe src="/path/to/your/pdf/script.php"></iframe>
    </body>
</html>