In last tutorial i showed how you can start working with Php Fedex API and create a simple Ship Service using the provided library. In this tutorial , I’m gonna show you how we can  generate a PDF using final response of Fedex Api and show that in the browser.

Where do i start ?

First of all , you need to open ship.php file where we have worked and start our codes in last tutorial by your code editor . If you check last line in the file, it dumps the responses from Api into the console as following :

var_dump($result);

when you run the code it returns confirmed data in Fedex that contains some other useful variables such as confirmed pdf content that we can use and show to users.

How to download PDF in browser ?

To achieve pdf content in the ship.php file , i use to store pdf content in a variable at the end of the file .

$pdf = $result->CompletedShipmentDetail->CompletedPackageDetails[0]->Label->Parts[0]->Image;

In order to download the file and put a name for our pdf , i define another variable that contains tracking number of our order.

$trackNum = $result->CompletedShipmentDetail->CompletedPackageDetails[0]->TrackingIds[0]->TrackingNumber;

Now we have the pdf content and the tracking number of our order in variables ,so we only need to tell browser that our content is pdf to parse as a pdf file.

$filename = 'order-'.$trackNum.'.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

Everything is ready to send the pdf content to user and make this ability to download via browser as following :

echo $pdf;

Final Codes

Here’s the final codes that we have written at top . please don’t forgot to comment the var_dump portion of last line of ship.php to prevent destroying your pdf content.

...
//var_dump($result);

$pdf = $result->CompletedShipmentDetail->CompletedPackageDetails[0]->Label->Parts[0]->Image;
$trackNum = $result->CompletedShipmentDetail->CompletedPackageDetails[0]->TrackingIds[0]->TrackingNumber;
$filename = 'order-'.$trackNum.'.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $pdf;
Show CommentsClose Comments

2 Comments

  • test1
    Posted February 27, 2018 at 1:02 pm 0Likes

    Very good written article. It will be supportive to anybody who usess it, as well as myself. Keep doing what you are doing – for sure i will check out more posts.

    • onlymaj
      Posted February 27, 2018 at 1:17 pm 0Likes

      You’re welcome. I’m happy to hearing that .

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.