Open FPDF in new tab

2020-07-17 04:08发布

问题:

I have a pdf generated (fpdf) from a post form. I would like the pdf to open in a new tab and/or window prompting the user to save the pdf. I'm guessing I need to save the output to a string

$data=$pdf->Output("OfficeForm.pdf", "S");

but what exactly can I do with this string to get it to open in a new window. I've attempted something like this but it's not working. Am I on the right track or is window.open not what I need?

echo "<script type=\"text/javascript\">      
window.open('$data', '_blank')    
</script>";

回答1:

If you use a form you can do it by specifying target='_blank' in the -tag (next to where you should have submit='something')

Example:

This will open a new Tab (showing whatever "makepdf.php" produces) on submit.

Hope it answers the question correctly



回答2:

Try $pdf->Output("OfficeForm.pdf", "I");



回答3:

I simply added target="_blank" to my form opening tag and used $_SESSION[]; to pass my form to the FPDF code:

<?php session_start(); ?>

<form id ="buildPDFform" name="buildPDFform"  target="_blank" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

...some code for my form

<input type="submit" name="buildPDf" id="buildPDf" class="buildPDFbutton" value="Build the PDF">
</form>

Then when the form is submitted I gather my form items, put them in an array, create a session the array goes into and use a header("Location: testcode.php") to redirect to where my FPDF code is.

if (isset($_POST['buildPDf'])) {
    $pdfArray = array();
    foreach ($_POST as $key => $value) {

        ...gather your form items into your array

    }
    $_SESSION['pdfArray'] = $pdfArray;
    header("Location: testcode.php");
}

And don't forget in your FPDF code file (testcode.php in my case) to grab your session that has the array.

<?php
    session_start();
    $pdfArray = $_SESSION['pdfArray'];

     ... your FPDF code

    $pdf->Output('I');
?>


回答4:

source: https://www.thesitewizard.com/html-tutorial/open-links-in-new-window-or-tab.shtml

use target="_blank" in your a tag to open it to new tab



标签: fpdf