androniennn
androniennn

Reputation: 3137

PDF data displayed but file not downloaded

I want to preview and download an order invoice pdf using this code :

public function generatePDFByIdOrder()
    {
        $order = new Order(1); //I want to download the invoice PDF of $order_id '1'
        if (!Validate::isLoadedObject($order)) {
            throw new PrestaShopException('Can\'t load Order object');
        }

        $order_invoice_collection = $order->getInvoicesCollection();
        $this->generatePDF($order_invoice_collection, PDF::TEMPLATE_DELIVERY_SLIP);
    }

    public function generatePDF($object, $template)
    {
        $pdf = new PDF($object, $template, Context::getContext()->smarty);
        $pdf->render();
    }

And calling it with the following code : $order = new order(); echo $order->generatePDFByIdOrder();

I have the pdf's data printed on the browser console but not downloaded . enter image description here

How can I manipulate that data to download a pdf file ?

Upvotes: 0

Views: 507

Answers (3)

Knowband Plugins
Knowband Plugins

Reputation: 1317

We recommend you, to create a separate controller to render the PDF file and to always open that controller in a new tab. It will help you to have separate logic using DOMPDF library.

Invoice controller will be as follows (invoice.php)

include_once(_PS_MODULE_DIR_.'supercehckout/libraries/dompdf/dompdf_config.inc.php');

class SuperCheckoutInvoiceModuleFrontController extends ModuleFrontController
{
    public function initContent()
    {
                parent::initContent();
                $this->generateInvoice(ORDER_ID);
    }
}

Note: SuperCheckout is the example module name.

generateInvoice() function will be as follows:

function generateInvoice($order_id)
{
        $dompdf = new DOMPDF();
        $html = utf8_decode(INVOICE_HTML);
        $dompdf->load_html(INVOICE_HTML);
        $dompdf->render();
}

Upvotes: 0

Julien Lachal
Julien Lachal

Reputation: 1141

I guess you only have to set the proper headers before rendering the PDF with TCPDF like so :

header("Content-type:application/pdf");

But "downloading" a PDF will depend on what the user's browser settings. It might download them (in which case you'd have to set another header called Content-Disposition:attachment) or display it inside the browser.

Upvotes: 0

Matteo Enna
Matteo Enna

Reputation: 1301

PrestaShop use TCPDF.

Edit generatePDF in this way:

public function generatePDF($object, $template)
{
    $pdf = new PDF($object, $template, Context::getContext()->smarty);
    $pdf->Output('name.pdf', 'I');
}

Upvotes: 1

Related Questions