简体   繁体   中英

FPDF with jQuery AJAX and window.open to open pdf on click of a link

I have a form, from this form I am using jquery AJAX to post the values to FPDF to store a pdf on the server, I then want the browser to open the pdf in a new tab using window.open(url, '_blank'), the window opens but the pdf is not loaded, I am left with the spinning loading icon forever, also upon reading the url with PHP's function is_dir() it returns false.. But the file is there in the FTP client and I can download it and read it.

Below is the page with the form:

jQuery(document).ready(function(){

    jQuery('.getPdf').live('click', function(event){

        event.preventDefault();

        var catId = jQuery(this).attr("id");

        jQuery('.removePost').remove();

        jQuery.ajax({

            type: "POST",

            url: "/wp-content/themes/weld-wide/ajax-post-to-pdf.php",

            data:jQuery('#form'+catId).serialize(),

            success:function(url){

                window.open(url, '_blank');

            },

     });

  });
});

Below is the ajax file that handles the creation of the pdf:

require ('fpdf/fpdf.php');//including the main class

//create FPDF object
$pdf=new FPDF();

//set document properties
$pdf->SetAuthor('Weld Wide');
$pdf->SetTitle('Weld Wide Metal Work');

//set text colour for the entire document
$pdf->SetTextColor(50,60,100);

//set up a page
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

//insert an image and make it a link
//$pdf->Image('../images/logo.png',10,20,33,0,' ','http://www.weld-wide.co.uk/');

//display the title with a border around it
$pdf->SetXY(65,20);
//$pdf->SetDrawColor(50,60,100);
//$pdf->Cell(100,10,'FPDF Tutorial',1,0,'C',0);

$pdf->Image('images/logo.png');

//Set x and y position for the main text, reduce font size and write content
$pdf->SetXY (10,50);
$pdf->SetFontSize(10);

$pdf->SetFont('Helvetica','B',12);
$pdf->Write(10, "Listed below are your selections for your metal work:\n");

$pdf->SetFont('Helvetica','',10);

foreach($_POST as $k => $v) {

$pdf->Multicell(0,7,str_replace('_',' ',$k)." : ".str_replace('-',' ',$v)."\n");

}

$rand = rand(1,1000000);

//Output the document F means save to server, D for download window popup
$pdf->Output('fpdf/temp_pdf/weld-wide-gate'.$rand.'.pdf','F');

$url = "http://www.".$_SERVER["HTTP_HOST"]."/wp-content/themes/weld-wide/"."fpdf/temp_pdf/weld-wide-gate".$rand.".pdf";

echo $url;

you don't need this line

$url = "http://www.".$_SERVER["HTTP_HOST"]."/wp-content/themes/weld-wide/"."fpdf/temp_pdf/weld-wide-gate".$rand.".pdf";

echo $url;

just remove it and add this to last line

$pdf->Output('fpdf/temp_pdf/weld-wide-gate'.$rand.'.pdf','I');

or

$pdf->Output('weld-wide-gate'.$rand.'.pdf','I');

and it will look like this

$pdf->Output('fpdf/temp_pdf/weld-wide-gate'.$rand.'.pdf','F');
$pdf->Output('fpdf/temp_pdf/weld-wide-gate'.$rand.'.pdf','I');

or

$pdf->Output('fpdf/temp_pdf/weld-wide-gate'.$rand.'.pdf','F');
$pdf->Output('weld-wide-gate'.$rand.'.pdf','I');

hope this will help you

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