0

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 ?

Matteo Enna
  • 1,285
  • 1
  • 15
  • 36
androniennn
  • 3,117
  • 11
  • 50
  • 107

3 Answers3

1

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');
}
Matteo Enna
  • 1,285
  • 1
  • 15
  • 36
0

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.

Julien Lachal
  • 1,101
  • 14
  • 23
0

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();
}
Knowband Plugins
  • 1,297
  • 1
  • 7
  • 6