Get page width and height of a pdf - php

I searched for a while, there is a simple way to get the page dimensions of a PDF (of each page)?
I'm not clear if TCPDF or FPDF allows that.

function toPixels($value){
//pt=point=0.352777778 mm, mm=millimeter=2.8346456675057350125948125904915 points, cm=centimeter=28.346456675057350125948125904915 points, in=inch=72 points=25.4mm
switch(PDF_UNIT){//http://www.unitconversion.org/unit_converter/typography.html
case "pt": return $value * 1.328352013;
case "mm": return $value * 3.779527559;
case "in": return $value * 96;
case "cm": return $value * 37.795275591;
}
return "TEST";
}
$dimensions = [//PDF_UNIT defaults to millimeters
"margins" => $pdf->GetMargins(),
"width" => $pdf->getPageWidth(),
"height" => $pdf->getPageHeight(),
];
$dimensions["width"] -= $dimensions["margins"]["left"] + $dimensions["margins"]["right"];
$dimensions["height"] -= $dimensions["margins"]["top"] + $dimensions["margins"]["bottom"];
$dimensions["width_pixels"] = toPixels($dimensions["width"]);
$dimensions["height_pixels"] = toPixels($dimensions["height"]);

You are in charge of setting the required Page dimension while creating an pdf object. For example the Fpdf sets per default an A4 Format, you may also adapt it to your current requirements by
new FPDF('P','your Unit [mm]',[width,height])
Those are your dimensions values which apply to every page of your pdf document.
Furhermore you may also set it for each of your page separatelly:
AddPage([string orientation [, mixed size [, int rotation]]]
And least but not last, there are alse the concept of margins, where you may draw your content.

Related

php spreadsheet getWidth return -1.0

I am using Laravel-excel in a Laravel project, and I need to export an excel with an image centered in B1, and because I needed other stylings I used WithEvents, and inside the registerEvents() function I try to get the column width of B as follow $event->sheet->getDelegate()->getColumnDimension('B')->getWidth(), but I get -1.0 in return which is not correct, also I used auto width columns because I needed that as follow:
foreach($event->sheet->getDelegate()->getColumnIterator() as $col)
{
$event->sheet->getDelegate()->getColumnDimension($col->getColumnIndex())->setAutoSize(true);
}
Note: $event->sheet->getDelegate() will return active sheet which is equal to $spreadsheet->getActiveSheet()
The -1 is the value for the demention of auto with size. If you will work with it you must have something like
$autowith = sheet->getDelegate()->getColumnDimension($col->getColumnIndex());
$calc=5
if($autowith == -1){
$calc += 22.2;
}else{
$calc += $autowith;
}

CakePHP Paginator: get first item from next page?

I have a table that contains different versions of a text. I want to display the diffs of each version with the previous version. I also want to paginate through the versions, in case there are more than 20. However, to diff the last text on each page I would need the first text of the next page. I cannot just make the page size one larger (21 in this case), because the second page would skip its first entity, and the third its first two etc.
$config = $this->Paginator->getConfig();
$this->Paginator->setConfig('limit', $config['limit'] + 1);
$inscriptions = $this->paginate($query);
I might instead be able to solve the problem by making a separate ->paginate() call for the single entity, but I would rather not execute a second query if possible.
$inscriptions = $this->paginate($query);
$config = $this->Paginator->getConfig();
$this->Paginator->setConfig([
'limit' => 1,
'page' => ($config['page'] * $config['limit']) + 1
]);
$inscriptions[] = $this->paginate($query)->first();
Is there a way to skip the first n results? In that case I could set the page size to 21 but set the page number to 1, and skip the first ((old page number - 1) * old page size) entities.
It is possible to make a custom paginator that extends the default one, but functions as described:
<?php
namespace App\Datasource;
use Cake\Core\InstanceConfigTrait;
use Cake\Datasource\Paginator;
use Cake\Datasource\QueryInterface;
use Cake\Datasource\RepositoryInterface;
class DiffPaginator extends Paginator
{
use InstanceConfigTrait;
protected function getQuery(RepositoryInterface $object, ?QueryInterface $query = null, array $data): QueryInterface
{
$data['options']['offset'] = ($data['options']['page'] - 1) * $data['options']['limit'];
$data['options']['limit'] += 1;
unset($data['options']['page']);
return parent::getQuery($object, $query, $data);
}
protected function buildParams(array $data): array
{
$paging = parent::buildParams($data);
if ($paging['current'] == $paging['perPage'] + 1) {
$paging['current'] -= 1;
}
return $paging;
}
}
In your controller then use the following:
$this->Paginator->setPaginator(new DiffPaginator);

PHPWord Full-width table with landscape orientation

In my PHPWord document I have a section with landscape orientation and table in it. I want the table to be full-width, but if I add table like this (since width is in percent according to the docs):
$table = $section->addTable(array('width' => 100));
or like this:
$table = $section->addTable(array('unit' => 'pct', 'width' => 5000));
my table is not full-width (actually, it seems that the table takes up the entire width, but for portrait orientation).
I can achieve the desired result in this way:
// Generate document
$word = new PHPWord();
$section = $word->addSection(array());
$sectionStyle = $section->getStyle();
$sectionStyle->setOrientation($sectionStyle::ORIENTATION_LANDSCAPE);
// Add table
$table = $section->addTable(array());
$tableStyle = $table->getStyle();
$tableStyle->setUnit($tableStyle::WIDTH_TWIP);
$tableStyle->setWidth($sectionStyle->getPageSizeW() - $sectionStyle->getMarginLeft() - $sectionStyle->getMarginRight());
But I doubt that this is good solution. Is there a better solution?

phpThumb - is it possible to add more than one filter using the setParameter method?

I'm building a thumbnail constructor with phpThumb (the James Heinrich implementation available here). Basically, I'm encapsulating the phpThumb class to build thumbnails with a syntax like this:
$thumbnail = \Helpers\Images::getThumbnail("/assets/images/sample.png", [
"width" => 150,
"filters" => [
"grayscale"
]
]);
This checks if the image I'm asking a thumbnail for with the given set of options and filters exists and, if it does, it just gives me the URL to this resource. In case it doesn't, it processes the image, save the resulting thumbnail and gives me the URL to this newly created resource.
So far, so good. My problem comes when I try to add more than one filter like this:
$thumbnail = \Helpers\Images::getThumbnail("/assets/images/sample.png", [
"width" => 150,
"filters" => [
"blur" => 25,
"grayscale"
]
]);
Internally, I do this:
/**
* more filter cases here
*/
} elseif ($filter === "blur") {
if (!empty($parameters)) {
if (sizeof($parameters === 1)) {
$value = current($parameters);
if (is_numeric($value)) {
if ($value >= 0) {
if ($value <= 25) {
$phpthumb->setParameter("fltr", implode("|", [
$filters[$filter],
$value
]));
}
}
}
}
}
} elseif ($filter === "brightness") {
/**
* more filter cases here
*/
$filters[$filter] is just an associative array with the different filter opcodes like usm (unsharpen), gblr (gaussian blur) and so on.
Seems like invoking the setParameter() method multiple times is not working like I want (or like it should).
Is there a way to stack different filters together using the OO approach?
Nevermind, I solved it by changing the core logic. Calling the setParameter() method out of the loop with all operations stored in array format fixed my problem.

PHP pchart 2.0 Additional labels aligned with each bar

Hi I have a pChart 2 bar chart which contains a variable number of bars and as well as the actual value for each bar I wish to display 2 additional values to the right hand side of each bar. Please consider the image below where the values in the rectangle are an example of the figures I need to add to the chart.
Obviously I want to make them look a bit neater than this but really at the moment I am just wondering
What would be the correct way to do this in pChart?
Is there a built in way to perform this kind of decoration?
If not, how should I best align my additional output with each bar in the chart, considering that these bars are variable in pixel size and data size (i.e. this has to be calculated not approximated for a single scenario).
Just for clarity the extra figures are simply the same scores from previous years and would be decorated in a colour border or similar and included in a legend.
Thanks.
In the drawBarChart() method of the file pDraw.class.php there is the following conditional at the end of that method:
if ($DisplayPos == LABEL_POS_INSIDE && abs($TxtWidth) < abs($BarWidth)){
...
else{
At the end of that else block is a $this->drawText() method, this outputs the value on the end of the bar and will be called for each bar in the series. You can also at this point write additional values to the end of the chart (in this case the right margin) using the following:
$itemGap = 45; //the space between items
$items = array(1,2,3); //the value to draw
$colStart = $this->GraphAreaX2 + $itemGap;
foreach($itemGap as $item)
{
$this->drawText($colStart, $Y + $YOffset + $YSize / 2, $item, array("R" => $DisplayR, "G" => $DisplayG, "B" => $DisplayB, "Align" => $Align, "FontSize" => $DisplaySize));
$colStart += $itemGap;
}
I hope that is helpful to someone.

Categories