Solved

convert xhtml to pdf !

Posted on 2013-06-11
2
1,777 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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now