Solved

convert xhtml to pdf !

Posted on 2013-06-11
2
1,914 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
2 Comments
 
LVL 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Row insertion failed. Array 5 48
Word, Excel, Access icons 4 17
Checking https returns 301 21 25
send email part1 9 28
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
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)

838 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