Solved

mpdf - tables not laid out properly

Posted on 2014-11-09
7
274 Views
Last Modified: 2014-11-09
I am using mpdf 6.0 with php and i am having a problem with tables. They look fine in the html then look crazy when converted to pdf

Here is the code

    <table width="100%">
            <tr>
                <td style = "text-align:center;border: 1px solid black; font-size:28pt;" width="80%" rowspan="2">Task Activity</td>
                <td    rowspan ="4"></td>
                <td style = "border: 1px solid black;" rowspan="2" width="10%">Risk Assessment no</td>
                <td style = "font-size:21pt;text-align:center;border: 1px solid black;" rowspan="2" width="5%">199999</td>
            </tr>
    
            <tr>
            </tr>
    
            <tr>
                <td style = "text-align:center;border: 1px solid black; font-size:24pt;" width="80%" rowspan="2">Site</td>
                <td style = "border: 1px solid black;">dc</td>
                <td style = "text-align:center;border: 1px solid black;">10/10/2000</td>
            </tr>
            <tr>
                <td style = "border: 1px solid black;">dr</td>
                <td style = "text-align:center;border: 1px solid black;">10/10/2000</td>
            </tr>
    </table>

Open in new window



Here is a website with screenshots of the output:
http://217.199.187.73/tolpitsmotors.com/index.html

Open in new window

The top one is the html
The second one is the pdf

Any help to fix this is much appreciated
0
Comment
Question by:andieje
  • 4
  • 3
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40431239
HTML and PDF are not quite as different as fish and bicycles, but almost that different.  HTML is a semantic markup language, intended to apply and imply the meaning of data elements in the electronic document.  PDF is a document layout language intended to format and position data elements on the printed page.  HTML knows nothing of format and position, and PDF knows nothing of semantic meaning.  For historical reasons that are obsolete today, HTML used to be impressed into formatting responsibilities, but we don't do that any more - we use CSS to create liquid and attractive formats that work in a variety of browsers and devices with different viewports.

Which brings us to the question of "what's the right tool?"  If what you're designing is the layout of a web page, the right tool is CSS.  If what you're designing is a printed document, the right tool is PDF.  These are two different applications, two different "views" of the information, if you subscribe to the concept of Model-View-Controller.

I've had very good results using FPDF and have a little experience with TCPDF (mostly as a test), but I've never found anything that dependably copies HTML markup and translates it into PDF.  Instead, I've always made the PDF view of the data into a separate project.  There are a lot of things in HTML that do not make sense on paper (links, for example) and these can be omitted.  Working with FPDF allows you very fine positioning - I believe that 1/100 inch is possible.  Pictures and fonts are available.  It's an all-around good tool for creating a printed document.  But it doesn't create an HTML document any better than HTML creates a PDF document.  Executive summary - forget the HTML markup and just create the PDF you need, directly, with one of the free and open-source tools.
0
 

Author Comment

by:andieje
ID: 40431284
I appreciate your comments but what tools create pdf directly? Do you mean fpdf? or something else?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40431288
Yes, FPDF is my favorite.  Download it here: http://www.fpdf.org/  

Here is my "hello world" example to help you get started.
http://iconoun.com/demo/fpdf_hello_world.php

<?php // demo/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
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $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
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN BLACK ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 24);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(  2,   2,   2);
$pdf->Cell(52, 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

0
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.

 

Author Comment

by:andieje
ID: 40431292
thanks - but is fpdf still supported? it hasnt been updated since 2011
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40431322
It's "supported" through the "Forum" links and through web sites like E-E. This is free, open-source software.  We are all kind of expected to know how to read the online manual, use it and how to extend the class, etc.  The last time I used it for a big project was in 2011, and it worked perfectly with PHP 5.3.  Nothing about PHP is likely to change in a way that would degrade FPDF, and if it did, there would be someone in the community that would step up with a change (it's on Google Code and GitHub).  Executive summary: I would not hesitate to rely on it for a project today.
0
 

Author Closing Comment

by:andieje
ID: 40431469
excellent - thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40431693
It's the right way to go.  A little more work up front, but much better results over time!

Thanks for the points and thanks for using E-E, ~Ray
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
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 video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

759 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

18 Experts available now in Live!

Get 1:1 Help Now