简体   繁体   中英

Output buffering alternative php

I'm trying to save the content of a file into a PDF using html2pdf , but the file has some PHP codes which need to be processed. I made some research and I found out that I had to use output buffering so that the PHP content in the file can be processed. So I did something like:

<?php 
    require_once('html2pdf.class.php');    

    ob_start();
    require_once('my_file.php');
    $content = ob_get_clean();

    // force download of $content to a PDF
    $html2pdf = new HTML2PDF('P','A3','fr', false, 'ISO-8859-1');
    $html2pdf->writeHTML($content);
    $html2pdf->Output('file_name.pdf', 'D');
?>

The file my_file.php is the file that has some PHP code and the HTML content that I wanna save to a PDF, and the variable $content is the actual content with the PHP codes processed and everything. This works fine on Apache, but not on IIS.

Does anybody know an alternative way to make this work witout using ouput buffering? I tried file_get_contents('my_file.php'); but my php contents in my_file.php do not get processed when I do so.

Please, I'm looking for ways to do this without output buffering so that it can work on any server. I'm not looking for answers telling me to change my IIS server configuration or to use something else other than html2pdf.

Thanks in advance for any help

如果您可以修改my_file.php的内容,则可以将所有文本放入那里的变量中,而不是直接输出它。

You can use PHP/PDF Library http://php.net/manual/en/book.pdf.php
And follow this example : http://php.net/manual/en/pdf.examples-basic.php
Hope that helps :)

The easiest approach would be to edit my_file.php so that rather than containing HTML it assigns the HTML content to a PHP variable. Then all you need to do is echo the variable.

//other PHP processing goes here, or anywhere else.

$someVar = "hello world";

$myHTML = "<html>My output: $someVar </html>";

echo $myHTML;

It's an ugly way of handling HTML output, and I'm not saying it's good programming, but if you want to avoid editing config files it would be quick and easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM