This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Display PDF in Browser

nsidev wrote on 2020-09-29:
I am VERY new to this utility so this might be a basic, simplistic question. But, I have tried multiple variations of the code and cannot get it to work like I want.

I have scripts that will build various reports on demand and export them to PDF. PDFCROWD does an excellent job of creating and delivering (automatically downloading) these PDF reports, but I want them to be displayed in a browser window where the user can then select to download it.


Process Steps:
1. Open a new browser window
2. build the HTML string with the report data
3. send it pdfcrowd for conversion
4. display in browser window


Here is a sample of what I currently have that will download the PDF to the client.


<?php
    session_start();
    if(isset($_SESSION['userLoggedIn']) && $_SESSION['userLoggedIn'] )
    {
        include_once $_SERVER['DOCUMENT_ROOT'].'/crm/inc/base.inc.php';
        include $_SESSION['myroot'].'/inc/htmLawed/htmLawed.php';


$html = '<!DOCTYPE html>
<html>
<head>
<style>
// Add some styling elements
</style>
<title>Open Order Report</title>
</head>
<body>'
.htmLawed(get_ship_history($d1, $d2, false, true)).'
</body>
</html>';
        try
        {
            require $_SESSION['myroot'].'/inc/pdfcrowd/pdfcrowd.php';
            // create the API client instance
            $client = new \Pdfcrowd\HtmlToPdfClient("xxxxx", "my super duper top secret api code");
            $client->setOrientation('landscape');
            $client->setPageSize('A3');
            $client->setMarginLeft('0.5in');
            $client->setMarginRight('0.5in');
            $pdf = $client->convertString($html);
            // set HTTP response headers
            header('Content-Type: application/pdf');
            header('Cache-Control: no-cache');
            header('Accept-Ranges: none');
            header('Content-Disposition: attachment; filename="ShippingReport.pdf"');
            echo $pdf;
        }
        catch(\Pdfcrowd\Error $why) {
            // report the error
            header('Content-Type: text/plain');
            http_response_code($why->getCode());
            echo "Pdfcrowd Error: {$why}";
        }
        exit;
    }
    else
    {
        header('Location:/crm/user/');
        exit();
    }
?>


Any help is appreciated.

Using pdfcrowd on a php based website.
support wrote on 2020-09-30:
Try to use content disposition inline to display PDF directly in browser:
header('Content-Disposition: inline; filename="ShippingReport.pdf"');
nsidev wrote on 2020-09-30:
That did it.

I knew it was something simple, thank you so much for the assistance.

R