When downloading a PDF file, create it if the file does not exist


After the data is saved in database I am generating PDF with below ajax code
I do not have any problem here.

      type: "GET",
      url: "dompdf/create_pdf.php",
      data: {"create_pdf_id" : <?php echo $lastInsertId; ?>},
      success: function(msg){

I download the previously created pdf file from the list of data saved on a page in the administration panel with the following code
I do not have any problem here.
<a target="_blank" href="../download_pdf.php?pdf_id=188">PDF</a>

	$sql = $pdo->prepare("SELECT * FROM table WHERE id=? ");
	$read = $sql->fetch();

	$pdfdosyatarih = $read['pdf_file_name'];
	$pdfdosyatarihi = substr($pdfdosyatarih,0,16);
	$dosya = PDFDIR."/".$read['pdf_file_name'];  
	$islem = fopen($dosya, "rb");     
	$icerik = fread($islem, filesize($dosya));
	header('Content-Type: application/pdf');
	header('Content-Transfer-Encoding: binary');
	$pdfdosyaadi = "pdf file name ".$pdfdosyatarihi.".pdf";             
	header('Content-Disposition: attachment; filename='.$pdfdosyaadi.'');
	header('Content-Disposition: filename='.$pdfdosyaadi.'');           
	echo $icerik;                 

When the download PDF button I want to do is clicked, if the file does not exist, I want it to re-create the PDF file and download it.
I want to do this without redirecting to any page while doing this.
I would also like to show the message “The PDF File You Want To Download Is Regenerating Because It Does Not Exist. Please Wait. This May Take Some Time” during the process

I have code to show notification message with ajax

// Wait message is shown as soon as the ajax process starts
var bekleme = jw("b bekle").baslik("Please wait......").icerik("The PDF File You Want To Download Is Regenerating Because It Does Not Exist.<br />Please Wait...<br />This May Take Some Time").en(250).boy(120).kilitle().akilliKapatPasif().ac();
  success: function(msg){
  bekleme.kapat(); //Wait message closes when successful

Thank you

Adem, I would do it in the PHP instead. Check if the file exists. If it does, then do not show the download-button at all. Just use the PHP file exists function to check and if it is NOT there, then show the download button. If it does exist, replace the download button with a notice that " This PDF already exists! ".
Or something similar. Does this make sense?

I couldn’t come up with any ideas
i need your ideas

With this answer, you reminded me of something missing for another topic, thank you.

These operations are in the Admin Panel.
When Quotation is Generated, these two files “Quote PDF” and “Product Layout Display PDF” are created and stored on the server

There is a section to delete old PDF files to reduce server load
The quotation pdf file is regenerated with the current exchange rate in each view of the Quotation.Therefore, there is no problem in deleting the old price offer PDF file.

The pdf file describing the layout of the products may have been deleted, but when the pdf file is needed to be downloaded, if it is not on the server, I want it to be created and downloaded again.

I don’t want to redirect page when click download PDF button
The PDF file describing the layout of the products may take a few minutes since it is more than one file.
It is impossible to wait this long without informing the user.
with a popup “Rebuilding Because Existing Project File Was Deleted. Please wait…”

Well, you can use JS to do an AJAX call. In that call, you can open a new window and you can use the file_exists function to see if the file exists or not. If it exists, you can force the download in the ajax-called-php file using OBJ and headers. If the file does not exist, you can rebuild the PDF there and force it to download. It would be a complicated routine, but, it can be done.

As a solution, I followed the path you pointed out and solved it.
When listing registered offers

I added PDF download url to the button
I added onclick="pdf_create('125') to create with ajax as it is not a PDF

I added PDF download link when PDF creation with Ajax was successful

  success: function(msg){
	location.href = "../download_pdf.php?download_pdf_id=" + pdf_id

Is the pdf download url code I’ve added to Ajax correct?

This is how I got the result I wanted

Yes, that sounds good. Glad you solved it. Good work!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service