amitdivya / dompdf

Automatically exported from code.google.com/p/dompdf
0 stars 0 forks source link

Specify PDF exact width and height #525

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We have a project where we have to render a PDF in an exact size, the problem 
is that the size (width and height) is calculated specifically for each 
PDF-being rendered. 

I have made some minor changes to the 0.5.2 code, that allows the user to 
specify the width and height as points rather than a pre-defined size such as 
"letter", "a4" etc. 

It might be of interest to you to implement this logic, or something similar, 
in a future version.

Se examples of my changes below.

Regards
Jonas A. Olstad

In the file dompdf.cls.php I have made a new version of the set_paper() method:

function set_paper(
  $orientation = "portrait", 
  $width = 0, 
  $height = 0, 
  $size = NULL) {
        $this->_paper_orientation = $orientation;

        // Use the custom width- and height settings.
        if (!isset($size) && $width > 0 && $height > 0) {
            $this->_paper_size = "custom|{$width}|{$height}";

        } else {
            // Use the specified pre-defined size settings.
            $this->_paper_size = $size;
        }
  }

In the file cpdf_adapter.cls.php I have made some changes to the __construct() 
method:

 function __construct($paper = "letter", $orientation = "portrait") {    

    if ( is_array($paper) )
        $size = $paper;
    else if (strpos($paper, "custom") === 0) {
        //
        // if a custom width and height has been specified by the user,
        // please use these settings instead of one of the pre-defined
        // paper-sizes.
        //
        $tmp = explode("|", $paper);
        $size = array(0.0, 0.0, $tmp[1], $tmp[2]);
    } else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) {
        $size = self::$PAPER_SIZES[$paper];
    } else {
        $size = self::$PAPER_SIZES["letter"];
    }

    if ( mb_strtolower($orientation) == "landscape" ) {
      $a = $size[3];
      $size[3] = $size[2];
      $size[2] = $a;
    }

    $this->_pdf = new Cpdf($size);
    $this->_pdf->addInfo("Creator", "DOMPDF Converter");

    // Silence pedantic warnings about missing TZ settings
    if ( function_exists("date_default_timezone_get") ) {
      $tz = @date_default_timezone_get();
      date_default_timezone_set("UTC");
      $this->_pdf->addInfo("CreationDate", date("Y-m-d"));
      date_default_timezone_set($tz);

    } else {
      $this->_pdf->addInfo("CreationDate", date("Y-m-d"));
    }

    $this->_width = $size[2] - $size[0];
    $this->_height= $size[3] - $size[1];
    $this->_pdf->openHere('Fit');

    $this->_page_number = $this->_page_count = 1;
    $this->_page_text = array();

    $this->_pages = array($this->_pdf->getFirstPageId());

    $this->_image_cache = array();
  }

Original issue reported on code.google.com by jonas.ol...@gmail.com on 2 Aug 2012 at 7:58

GoogleCodeExporter commented 9 years ago
If you can we recommend you upgrade to 0.6.0 beta 3. It's just as stable as 
0.5.2 and has a lot more features. Plus, it has the benefit of support for 
custom paper sized built in. You would just call the set_paper() method before 
rendering your document:

  $dompdf->set_paper(array(0,0,$width,$height));

$width & $height are the width and height of the page size in points, 72pt per 
inch.

Original comment by eclecticgeek on 2 Aug 2012 at 3:54

GoogleCodeExporter commented 9 years ago

Original comment by eclecticgeek on 30 May 2013 at 5:16