2

I'm having a lot of issues writing database results to a PDF using FPDF, it seems there is either unsupported data or characters or ~something~ in the data being returned from the database.

  • the PDF file gets written and downloaded with no errors, but occasuionally I get a page that looks like:

enter image description here

The asterisks are missing pages. now, I'm trying to sanitize the data by running each item through a function:

public function cleanPDFData($data){

        $data = utf8_decode($data);
        $data = trim($data);
        $data = preg_replace( "/\r|\n/", "", $data);

        return $data;

    }

But I am still getting broken pages.

  • If I replace the query results with a string, the PDF is generated perfectly [i.e. return "test"; ] the error is in the data itself

  • if I view the record in the database, nothing appears out of sorts, correct data types, no funny characters, no missing data. I don't know what else to look for.

  • if I start deleting records before the blank pages, the PDF will again generate itself perfectly.

  • no errors are thrown

I am out of ideas, my best guess is that my php script is trying to pass some sort of unsupported character or data to the FPDF - but I have no idea what it might be!

here is the actual PDF : sample-pdf.pdf

UPDATE

ok - I think I found part of the problem, here is the updated cleaning function:

public function cleanPDFData($data){

        if(strlen($data) <= 0){$data = '-';}

        $data = trim($data);
        $data = str_replace('/', '-', $data);
        $data = iconv('UTF-8', 'ISO-8859-1', $data);
        $data = preg_replace( "/\r|\n/", "", $data);

        return $data;

    }

So once the "/" characters are removed, the PDF will generate properly, but now I am left without the [sometimes] needed slashes (/). How can I generate the PDF ensuring that all the special characters are correctly displayed?

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
  • Do you get any errors from FPDF? There should be some information from the lib that shows where the problem is exactly. Data in the screens seems simple so it's even wierder. – LukasS Nov 16 '14 at 21:15
  • Have you tried viewing the PDF source? http://stackoverflow.com/questions/6562235/how-to-open-pdf-raw – ymas Nov 16 '14 at 21:16
  • @Lukas - no errors are thrown at all [by anything] – Sean Kimball Nov 16 '14 at 21:33
  • @ymas ... hmm, I can try, but I'm not sure what that's gonna get me. Don't know anything about the internals of a PDF. – Sean Kimball Nov 16 '14 at 21:41
  • @SeanKimball errors might be logged to apache log file, check them too. – LukasS Nov 16 '14 at 21:43
  • @lukas, nope, been tailing that log hoping for something. – Sean Kimball Nov 16 '14 at 22:01
  • @SeanKimball it'll provide a clue about what is causing the error. The PDF reference is not particularly complex. If possible, can you provide a link to a problematic PDF? – ymas Nov 16 '14 at 22:08
  • sure can: www.globalstainlessauctions.com/sample-pdf.pdf – Sean Kimball Nov 16 '14 at 22:12
  • @SeanKimball can you please turn the compression off? I'm going to uncompress the document myself, but it would be great if you could do that too. – ymas Nov 16 '14 at 22:48
  • here is a complete file with compression off [I think] I added $pdf->setCompression('false'); [I think that will do it?] globalstainlessauctions.com/746-globalstainless-search-results.pdf – Sean Kimball Nov 16 '14 at 22:54
  • There are really syntax errors in the pages content streams. You will see tokens like `B0.54` or `331.5Q` or `(6120 331.5Q` in them. Are you using the latest version of FPDF? If yes, we would need some more code. Are you using any special extension? – Jan Slabon Nov 17 '14 at 07:50

1 Answers1

1

the problem turned out to be compression.

You can disable compression via:

$pdf->setCompression(false);

Thanks to Olivier on the FPDF forums.

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73