CamilleFF
CamilleFF

Reputation: 90

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?

Upvotes: 0

Views: 3891

Answers (1)

CamilleFF
CamilleFF

Reputation: 90

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 on the first output.
So, when I need to backup, I use a Jquery function to execute the PHP code in the background and upload the PDF file (hidden iframe works too).

Below the end of my PHP code:

<?php
...
if (mycondition) {$string='F';}
else {$string ='I';}
...
$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">
    var phpfile = <?php echo json_encode($Filename); ?>;  // the php file to upload
    function uploadpdf() {
    $.get(phpfile);
    return false; }
</script>  

Hope this will help!

Upvotes: 2

Related Questions