简体   繁体   中英

PHP file_get_contents for dynamically generated urls/pdf

I want to loop through an array of urls and create a single zip file. The urls are dynamically generated pdf files/invoices. The programme works fine for physically existing files but for the dynamically generated ones, it gets the html source code/page not the pdf.

Is there an alternative to using : file_gets_contents ?.

Below is a simulation code:

// prepare array of URL's/files

$files = array(
     "1" => "http://localhost/app/addon/test.pdf", //works
     "2" => "http://localhost/app/addon/test2.pdf",//works
     "3" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=6", //doesn't work
     "4" => "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=4",
     "5"=>  "http://localhost/app/index.php?section=flight-fatturazione&option=getPdf&tipo=invoice&id=2"
);

// create new zip opbject
$zip = new ZipArchive();

// create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);

// loop through url/file array
foreach($files as $file){
   // download file
   // this gets file contents, what to do when file is created on the fly
   $download_file = file_get_contents($file);
   // add it to the zip
   $zip->addFromString(basename($file),$download_file);
}
   // close zip
$zip->close();

   // send the file to the browser as a download
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($tmp_file);

Currently, the code creates a zip with the with 2 pdf file(ie the file physically sitting server :test.pdf, test2.pdf) and 3 html files even though accesing the urls directly generate pdfs.

You're saving the files 3-5 as .php-files. Does renaming them to .pdf work?

If, as you say, you're receiving html files, do they contain the PHP-code to generate them? (As in, is it returning the code of index.php?) Curious to see if they're actually getting processed. If these both don't work, do you need specific cookies or other settings (session?) in order to make a request to get the pdf?

After going around for a long time and trying different solutions, I solved the problem. As I mentioned in my question, I was generating invoices on the fly and trying to get the contents using

file_get_contents

by passing the url of the generated invoice. This returned html pages in the zip instead of the pdf. There were suggestions to use curl and simulate an http request but this did not solve the problem. All this was to avoid code duplication in my application, but in the end I had no option.

So for each invoice i call function that creates it dynamically and then i get the content with file_get_contents

function createInvoiceContent($invoice_id){

   require_once('tcpdf_include.php');

   // create new PDF document
   $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

   /* Do a bunch of stuff to create the invoice */



   // Close and output PDF document
   // This method has several options, check the source code documentation  for more information.
   $invoice_content = $pdf->Output('invoice_name.pdf', 'I');
   return $invoice_content;
}

The i can call this function when i need it.

   //get pdf invoice  file
    $invoice = file_get_contents(createInvoiceContent($invoice_id));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM