CamilleFF CamilleFF - 2 years ago 545
PHP Question

Get and save the content generated by PHP using FPDF

I use the FPDF library to display online PDF files, with the ouput option « I » (which output the content to browser).

Then, I need to be able to save some of those generated pdfs on my server (like does the ouput option « F »).

In order to do that, I wrote this script:

$pdf_data = file_get_contents('https:/HOST/Folder/file.php');
$path =__DIR__.'/Folder/file.pdf';
file_put_contents( $path, $pdf_data );

It succeeds to create a file with pdf extension to the directory, but :

- I got an error: « Format error : not a pdf or corrupted » when I try to open the "pdf" file,

- The saved file is empty (?).

So, what is wrong?

How can I get the content generated by the php file, and save it as a pdf on my server?

Answer Source

Finaly, I forgot this script and found a new solution that works!

I set up two outputs into my PHP code build with FPDF, one to display the file online and one to save the file on the server.
The content generated by the PHP code is always displayed, and I control the PDF backup with a string. So, when I need to backup, I use a Jquery AJAX function to execute the PHP code in the background and upload the PDF file.

Below the end of my PHP code:

if (mycondition) {$string='F';}
else {$string ='';}
$pdf->Output(__DIR__.'/../../Folder/file.pdf',$string);  // save on the server
$pdf->Output('file.pdf','I');  // display on the browser

Below the jQuery function I use:

<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
    function uploadpdf() {
    $.get("<?php $Filename ?>.php");
    return false; }

Hope this will help!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download