Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

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

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Also, you might want to think about this.
http://refactoring.com/
Avatar of andieje
andieje

ASKER

Its not just missing images - its text too
Avatar of andieje

ASKER

Its the contents of entire pages
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?
https://www.experts-exchange.com/questions/28535541/images-used-in-pdf-generation-on-cloud-stored-in-separate-subdomain.html
Avatar of andieje

ASKER

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
Avatar of andieje

ASKER

Always the same error
Avatar of andieje

ASKER

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
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?
Avatar of andieje

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andieje

ASKER

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?
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.
Avatar of andieje

ASKER

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
Avatar of andieje

ASKER

The pdf in this file i was given to work with was corrupt and couldnt be verified. Please can i lose this question
Avatar of andieje

ASKER

thnks ray
Avatar of andieje

ASKER

thanks