views:

113

answers:

3

Hi! I'm currently writing a code to output a pdf file in PHP using PDFlib from http://www.pdflib.com/. The problem is all html tag is also written in the output file. How can be able to cancel out all those tags?

Here is my sample code.

            $postVariable = $_POST;

        $contentData = "";
        foreach($postVariable as $key => $value){ 
            if(is_array($key)){
                foreach($key as $key1 => $value1){

                   $contentData.= $key1 .": ". $value1."<nextline>";

                }
            }else{

                $contentData.= $key .": ". $value."<nextline>";

            } 
        }
        $testdata = nl2br($contentData);
        $pdf = pdf_new();
        // open a file
        pdf_open_file($pdf, $_SERVER['DOCUMENT_ROOT']."cas".DIRECTORY_SEPARATOR."$filename.pdf");
        // start a new page (A4)
        pdf_begin_page($pdf, 595, 842);

        // Locate Font Directory
        $fontdir = "C:\WINDOWS\Fonts";
        // Font Name Parameters
        pdf_set_parameter($pdf, "FontOutline", "arialMyName=$fontdir\arial.ttf");
        // Find Font
        $arial = PDF_findfont($pdf,"arialMyName","host",0 );
        // Set font size and font name
        pdf_setfont($pdf, $arial, 10);

        //$arial = pdf_findfont($pdf, "Arial", "host", 1);
        //pdf_setfont($pdf, $arial, 10);
        // print text
        pdf_show_xy($pdf, "TO THE UNIT OWNER",50, 750); 
        pdf_show_xy($pdf, "Test ext", 50,730);
        pdf_show_xy($pdf, "test test", 50,715);
        pdf_show_xy($pdf, $contentData, 50,700);
        // end page
        pdf_end_page($pdf);
        // close and save file
        pdf_close($pdf);

and the sample output is: TO THE UNIT OWNER Test text test test type: Apartment**var_name: **var_company: **var_date: **submit: Save and Download<

It disregards the html tags and it include it on the content.

Is there any other methods on how to print out HTML to PDF using the library that I'm currently using (PDFlib).

Thanks.

regards, Resty

+2  A: 

You cannot generate PDF content from HTML by using PDFLib. Ok, you can write convertor, but that's a huge work to do.

I recommend using TCPDF which supports HTML input.

Māris Kiseļovs
+1  A: 

Man... I was trying to do same thing once. It is very hard to work with pdflib directly.

I found this project, hosted on Google code site. The project deserved 10 stars out of 5.

It is called DOM PDF, you can download it here. You can generate PDFs from HTML pages, with CSS support. You can generate PDF with less than 10 lines of code!

Look at samples to see it in action.

Alex
+1  A: 

Hi Resty.

I would recommend the TCPDF library. It can convert your HTML to a PDF file, including CSS code. It has support for quite a lot of HTML tags, and does a decent job. HOWEVER, from personal experience, while it is very possible to generate high quality PDF files with it, the results are not always 100% as expected. It might need a bit of tweaking and fiddling to get the result you want.

If you only want to remove the HTML tags, I think you might want to take a look at strip_tags. But I guess that's not really what you're after.

Finally, there's a really really cool new kid on the block - a PHP extension that uses the WebKit HTML and rendering engine to generate PDFs. It is called the libwkhtmltox extension and can be found here. It produces amazing results. For example, look at this PDF it made from the homepage of the New York Times - see (http://www.2shared.com/document/kYuS_G7p/nytimes.html - click "save to my pc" down below). That output was generated without specifying any additional options. Exceptional. HOWEVER, you need to get it running as PHP extension, and that might be a non-trivial task. So I would say: stick with the TCPDF library for now.

Edward
Thank you guys for sharing your knowledge. I will try TCPDF.
Resty Sajagon
I wish I could give more then one up-vote men. Thanks a lot.
Cesar