Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf Knp-snappy custom height/width which unit is expected?

Tags:

php

pdf

symfony

I´m generating pdf´s from html-pages and i want to set a specific width and height to each page of the pdf, i found nothing on the web, but in the bundle theres a pdf.php with

protected function configure() 

where these configurations are :

        'page-height'                  => null,
        'page-size'                    => null,
        'page-width'                   => null,

so i dont know which unit is expected

so if i set it to

                'page-height' => 600,
                'page-width'  => 1000,

where i generate the pdf, it gets much too large, so it cant be pixels maybe it has to do with any other option ? heres my call :

          $pdfString=$this->knp_snappy->getOutputFromHtml($html, array(
                'orientation' => 'landscape', 
                'enable-javascript' => true, 
                'javascript-delay' => 1000, 
                'no-stop-slow-scripts' => true, 
                'no-background' => false, 
                'lowquality' => false,
                'page-height' => 600,
                'page-width'  => 1000,
                'encoding' => 'utf-8',
                'images' => true,
                'cookie' => array(),
                'dpi' => 300,
                'image-dpi' => 300,
                'enable-external-links' => true,
                'enable-internal-links' => true
            ));

I need to have a specific height because now the charts i draw are cut because i dont know the exact height of the generated pdf

for any help, thanks in advance!

like image 283
john Smith Avatar asked May 14 '14 09:05

john Smith


2 Answers

--page-width and --page-height take real world units (as in cm, not pixels)

--page-size takes values like "A4", "A5", "Letter", etc.

Although it's probably easier not to touch these values and just play around with the layout you're using, or maybe use --zoom, but I'd definitely go for tweaking the layout (maybe have a layout or CSS file specific for generating the PDF)

like image 124
Juanjo Avatar answered Oct 21 '22 21:10

Juanjo


you don't have to touch the class Pdf inside the bundle just in the controller you have to add the options when you call to the methode

$response->setContent($this->get('knp_snappy.pdf')->getOutputFromHtml($html,array('page-height' =>  200,'page-width' => 50)));

and this is the hole controlller acation

/**
 * Displays a form to create a new Delivery entity.
 *
 * @Route("/generate/pdf/ticket/{id}", name="delivery_show_pdf_ticket")
 * @Method("GET")
 * @Template("ShippiesServiceWebAppBundle:Delivery:ticket.html.twig")
 */
public function generatePDFTicketAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('ShippiesCoreBundle:Delivery')->find($id);


    $info = array(
        $entity ->getClient()->getCustomerNumber(),

    );
    $infoText = implode('|', $info);

    $options = array(
        'code'   => $infoText,
        'type'   => 'datamatrix',
        'format' => 'png',
        'width'  => 3,
        'height' => 3,
        'color'  => array(0, 0, 0),
    );

    $barcode =
        $this->get('sgk_barcode.generator')->generate($options);

    $request = $this->getRequest();
    $html = $this->render('ShippiesServiceWebAppBundle:Delivery:ticket.html.twig', array('entity' =>  $entity,'info'=>$infoText,'barcode'=>$barcode,'base_dir' => $this->get('kernel')->getRootDir() . '/../web' . $request->getBasePath()));
 $response = new Response();



  $response->setContent($this->get('knp_snappy.pdf')->getOutputFromHtml($html,array('page-height' =>  200,'page-width' => 50)));
 $response->headers->set('Content-Type', 'application/pdf');
   $response->headers->set('Content-disposition', 'filename=1.pdf');

    return $response;

}
like image 2
Baya Houda Avatar answered Oct 21 '22 20:10

Baya Houda