简体   繁体   中英

Open PDF in new tab using PHP

I am simply trying to open a .pdf in another tab.

I start by using jQuery to post a couple of variables:

$('#resultsTable').on('click', 'tr > td > .view-pdf', function()
{
  var $dataTable = $('#resultsTable').DataTable();
  var tr = $(this).closest('tr');
  var rowBookingNum = $dataTable.row(tr).data().JOB_REFERENCE;
  var rowPartnerNum = $dataTable.row(tr).data().SHIPPER_CODE;

  $.redirect('process/viewpdf.php', {'bookingnum':rowBookingNum, 'partnernum':rowPartnerNum });
});

The PHP script looks like this:

<?php
if(isset($_POST["bookingnum"]))
{
    $bookingnum = $_POST["bookingnum"];
    $partnernum = $_POST["partnernum"];

    $dir = "D:/CargoDocsPDFs/" . $partnernum;

    $file = $dir . "/" . $bookingnum . '.pdf';  

    if (file_exists($file)) 
    {
        header('Content-type: application/pdf');
        header('Content-Disposition: inline; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }           
}
?>

Using all of the above, I can get the .pdf to open in the same window. This causes the user to have to click the BACK button to go back to the previous screen.

I need the .pdf to open in a new tab. How can I adjust my code to make this happen?

You need to specify a target in $.redirect(url, [values, [method, [target, [traditional, [redirectTop]]]]])

See usage: http://github.com/mgalante/jquery.redirect

Desired usage:

$.redirect('process/viewpdf.php', {'bookingnum':rowBookingNum, 'partnernum':rowPartnerNum }, 'POST', '_blank');

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