?
Solved

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

Posted on 2014-10-11
17
Medium Priority
?
133 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
[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
  • 11
  • 6
17 Comments
 
LVL 111

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 111

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

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

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
 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

741 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