Solved

convert xhtml to pdf !

Posted on 2013-06-11
2
2,046 Views
Last Modified: 2013-06-20
Hi Experts,

In my website i'm using html2pdf convertor of http://sourceforge.net/projects/html2fpdf/?source=dlp.

This is working good html tags  convert to pdf format.. but most inline css and styles elements not converting to pdf, so unable to got a absolute result and styles..
what is the problem? or xhtml not supporting convert of html2pdf format? Plz guide me.

Example css code :
<style type="text/css">
<!--
tr.form-tab-outer td
{
border-bottom: 1px solid #cccccc;
}
tr.form-tab-outer td tr td
{
border-bottom:0px;
}
-->
</style>

Open in new window


HMTL CODE
...
...
<tr class="form-tab-outer">
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><b>ORDER NO:</b> '.$row['order_no'].'</td>
        </tr>
      </table></td>
  </tr>
...
...

Open in new window

0
Comment
Question by:codeoxygen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39237383
The central issue here is that HTML, and/or XHTML, are semantic markup languages.  They are meant to attach meaning to data elements, but they have nothing whatsoever to do with the positioning and appearance of these data elements.  That part of markup is handled via CSS.  Historically, many web developers have used HTML to position data (think tables) but that has been, and remains, a misuse of the markup language.  Unfortunately, there have been no strict standards in browser implementation of the rules, so the online world is full of polluted documents.

PDF is a document layout language.  It is for paper, meant to provide detailed positioning and appearance information for data elements on the printed page.

HTML and PDF are not quite as different as fish and bicycles, but they're almost that different since they're used for totally different things. If there is any similarity to be drawn between online layout and paper layout, it would be some kind of analogy between CSS and PDF.  But as far as I know, no such software exists today.

The only workable solution I have found is to use two separate presentation layers.  For online display, HTML and CSS makes sense.  For paper presentation, PDF makes sense.  This is why the Model-View-Controller design pattern is so valuable.  It allows you to separate the View and extend the View with the correct code set, depending on whether the script is expected to produce HTML or PDF.

FPDF and TCPDF are free and open-source classes that will build PDF documents for you.  Either one can probably do what you want.  Initial document layout is a meticulous and painstaking exercise, since these programs can position to 1mm.  But once the View is set up, they are both lightning fast and accurate.  Here is the Hello World exercise in FPDF.

<?php // RAY_fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE SOME OF THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

Best of luck with your project, ~Ray
0
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 250 total points
ID: 39240384
There are several products ou there to help you in that. The most reliable one is prince, although the most costly one.

Then there is html2pdf, dompdf, wkhtmltopdf, etc.


Resources:
http://www.princexml.com/
http://www.tufat.com/s_html2ps_html2pdf.htm
https://code.google.com/p/dompdf/
http://www.dancrintea.ro/html-to-pdf/
https://code.google.com/p/wkhtmltopdf/
http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question