Solved

php pdf generators losing content when saved (content visible on screen)

Posted on 2014-10-11
17
122 Views
Last Modified: 2014-12-07
Hello

I have inherited a website which makes lots of pdf files. Luckily for me sometimes it uses fpdf, sometimes tcpdf, sometimes mpdf. Its a joy to wade through.

But i find this curious. The website generates the pdf files (haphazardly but they are made) but then when the files are saved or emailed sometimes bits of the document got missing like a logo or a contents page.

I would have thought that once the file had been generated that was it and everything was complete. I dont understand why emailing or saving the file loses information.

Because the code is is such a mess I am hoping you might give me some ideas before i post sections that might be relevant as its very hard to tell

But in theory before looking at this code. How is it possible that saving a pregenerated pdf can lose data?

Thanks
0
Comment
Question by:andieje
  • 11
  • 6
17 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375000
How is it possible that any programming error could occur?  I have never seen this happen in a PDF, but I have seen plenty of HTML emails with links that didn't work because the programmer used relative paths, and when the email was opened on a client's machine those paths didn't point to images any more.   So it could be something like a local dependency.  Steps to try to nail this down...

1. Is it always the same PDF(s) that is missing an image?
2. Can you reproduce the missing images regularly?
3. Once we know that, we can start looking at the PDF files to see if there is something unique about them.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375002
Also, you might want to think about this.
http://refactoring.com/
0
 

Author Comment

by:andieje
ID: 40375009
Its not just missing images - its text too
0
 

Author Comment

by:andieje
ID: 40375011
Its the contents of entire pages
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375033
Interesting.  Is it consistent in any way - ie, can you reproduce the failure by rerunning the same script?  Or is it sporadic?

Is it the same site that you mentioned in this Q?
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28535541.html
0
 

Author Comment

by:andieje
ID: 40375037
Yes

The logo is always missing

There  is a graphic present always

There is a swirl drawn on the page and that is always there
0
 

Author Comment

by:andieje
ID: 40375038
Always the same error
0
 

Author Comment

by:andieje
ID: 40375040
just to be clear you seee everyhing
- logo
-content
page swirl

When document is saved logo disapears (probably a path issue)
the text in the contents is links/bookmarks to pages in the document
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375051
Well, since I haven't seen this code, but have seen lots of examples of really badly written code I'd like to suggest that you consider refactoring.  The site obviously does not work and probably never did, and trying to "fix" bad code usually takes longer than starting over and doing the job right - with the right data in the right places and with the right test cases along the way.

Do you know the history of the site?  Why does it use three different classes to generate PDFs?
0
 

Author Comment

by:andieje
ID: 40375067
no idea! if you were me and starting again what pdf generator would you use. It doest have to be free, quality is the issue
thanks
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40375071
I have had excellent results with FPDF, creating post cards, business cards, etc.  I've done a few scripts with TCPDF and found its documentation lacking a bit.  It seems to work, but there is a good bit of trial and error with any of these PDF extensions.  All of them seem to work OK at PHP 5.3, but that's out of date now, so I would want to set up a test case to exercise every feature I needed before I commit to development with either one of them.  I haven't used MPDF; it sounds like a seductively good idea, but the truth is that HTML is for semantic markup, and PDF is for document layout.  I just don't trust the process of trying to marry the two into one.
0
 

Author Comment

by:andieje
ID: 40375098
progress - when the logo is removed the contents is visible on save but then add logo and content vanishes and logo is visible when you save (all the document including logo is visible in browser)

Its weird . Its finei n the web browswer but not on saving. could it be anything to do with page margins or something like that?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40375234
I don't know what might be wrong.  I think at this point we can't make any progress without seeing the code and the test data.
0
 

Author Comment

by:andieje
ID: 40376064
My understanding - html2pdf makes a document to be converted to pdf. From my code this is some template code for one of the pages in the document


<page backimg="<?php echo $image_dir . 'backgrounds/' . $style['back']; ?>" backimgx="center" backimgy="bottom" backimgw="100%" backtop="30mm" backleft="10mm" backright="10mm" backbottom="12mm" footer="" style="font-size: 12pt; line-height:130%;" >
 
<page_header>
        <div class="pagehead">
            <table style="width:100%;">
                <tr>
                    <td style="width:70%; text-align:left;vertical-align:top; padding-top:22px;"> Safety Pack - Contents </td>
                    <td style="width:200px;text-align:right;vertical-align:top;"></td>
                </tr>
            </table>
        </div>
    </page_header>
    
    <page_footer>
        <div class="footer">
            <div <?php if (strlen($tagname) > 30) echo 'style="font-size:10pt;"';?>><?php echo $tagname ?></div>
            <div style="font-size:10pt; padding-top:10px;">Page [[page_cu]] / [[page_nb]]</div>
        </div>
    </page_footer>  
 </page>

Open in new window


I know now the developer doesnt have to put the page content in here. Bits and pieces are read in all over the place in buffers



Then this is the page that makes the pdf document. It is my understanding that the pdf header (not page header) is constant on every page

The page makes the html to be converted to pdf from all over the place including the template files

ob_start();
include(dirname(__FILE__) . '/res/front.php');
$content[$ri++] = ob_get_clean();

Open in new window




Here is the pdf header
Here is adding the logo. I out it local to be sure
  $header_img_path = "logo.png";
    //$header_img_path = PUBLIC_DIR_UPLOAD . 'logo/' . $company_logo;
    $html2pdf->pdf->Set_Header_IMG_Path($header_img_path);
    $html2pdf->pdf->setPrintHeader(true);

Then it writes the document from the content array

When something is wrong neither the pdf header nor the links show on that one page

Then on the next page its there again

Ironically the whole document displays fine in firefox as a standalone document and sumatrapdf reader
0
 

Author Comment

by:andieje
ID: 40388830
The pdf in this file i was given to work with was corrupt and couldnt be verified. Please can i lose this question
0
 

Author Comment

by:andieje
ID: 40388831
thnks ray
0
 

Author Closing Comment

by:andieje
ID: 40485718
thanks
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 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

12 Experts available now in Live!

Get 1:1 Help Now