How fix border in TcPDF writer in PhpSpreadsheet? - php

I've a PhpSpreadsheet process to export data to XLSX and PDF. I'm using TcPDF as PDF writer. But TcPDF has a problem to convert/translate border style.
Xlsx export
TcPDF export
Any idea to fix this?

I guess you have to use TcPDF's SetLineStyle() (see example 35):
$pdf->SetLineStyle(array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 4, 'color' => array(255, 0, 0)));
$text="DUMMY";
$pdf->Cell(0, 0, $text, 1, 1, 'L', 1, 0);
if you want to create complex border styles for individual cells check this
or see example 48 if you use HTML tables.
Alternatively, try SetLineWidth(1.0).

Related

How can draw dotted line in pdf using php and TCPDF?

I need to draw a horizontal dotted line in pdf using TCPDF. I tried:
$style = array('width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 1, 'color' => array(0, 0, 0));
$pdf->Line(5, 50, 100, 50, $style);
I used the dash parameter. When I set it to 1 it draws short dashes, but I need dots. I did not find an explanation of the style parameters or any manual on the internet.
Please use the below code for the draw horizontal dotted line in pdf using TCPDF and apply a style to it.
<?php
require_once('tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// disable header and footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
//set 'width' => 0.1, 'dash' => '1,1,1,1' as per your requirement
$style = array('width' => 0.5, 'dash' => '2,2,2,2', 'phase' => 0, 'color' => array(255, 0, 0));
// Line
$pdf->Text(5, 4, 'Line examples');
$pdf->Line(5, 20, 200, 20, $style);
//Close and output PDF document
$pdf->Output('line_example.pdf', 'I');
I hope this helps!
You can have dots, if you want to.
Just set the cap to round in the style properties:
$style = array('width' => 0.5, 'cap => 'round', 'dash' => '2,2,2,2', 'phase' => 0, 'color' => array(255, 0, 0));

How to add a custom paper size on FPDF?

I am using FPDF to create a document. My problem is the paper size requirement is 8.5 by 13 inches. Is there a way to add a custom page size. I tried searching still no avail. When I used 'Legal' the printing is warped.
$pdf = new myPDF('P','in',[8.5,13]);
$pdf->AddPage('P', 'Legal', 0);
$pdf->SetMargins('15', '20', '15');
$pdf->SetFont('Arial', 'B', 12);
$pdf->Ln(5);
$pdf->Cell(0, 5, 'PAGE 1', 0, 1, 'C');
$pdf->AddPage('P', 'Legal', 0);
$pdf->Cell(0, 5, 'PAGE ', 0, 1, 'C');
As the documentation says, when you create the instance the 3rd parameter may be an array that specifies the width and height. So you may use:
$pdf = new myPDF('P','in',[8.5,13]);

TCPDF draw circle with border and fill

I'm using TCPDF in my php application, but I'm not finding a way to draw a circle with black border and filled with another color;
here my code:
$style_bollino = array('width' => 0.25, 'dash' => 0, 'color' => array(0, 0, 0));
$this->SetAlpha(1);
$this->Circle(35, 100, 4, 0, 360, 'C', $style_bollino, array(210, 0, 0));
I tried also to change 'C' parameter to 'F' or null but I didn't get the result.
I'm not able to figure out what I'm missing
kind regards,
Matt
According to one of their examples, there's a one-liner way to do it, just pass 'DF' to $style parameter:
$this->Circle(35, 100, 4, 0, 360, 'DF', $style_bollino, array(210, 0, 0));
For a list of options for this parameter, check the PHPDoc in TCPDF_STATIC::getPathPaintOperator() function.
This seems like a fairly straightforward requirement but I can't work out how to do it in one line either.
However, it's pretty simple to fill the circle and then draw another stroked circle on top of it.
foreach (array("F", "S") as $fill_style) {
$this->Circle(35, 100, 4, 0, 360, $fill_style, $style_bollino, array(210, 0, 0));
}

TCPDF can see line in pdf generated but not in printout

$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$pdf->Line(5, 10, 80, 30, $style);
I uses the above statements to draw a line.
Result view in screen has this line drawn.
However It's not printed on paper.
Any help would be highly appreciated.
I'm sorry folks, it's not TCPDF problem.
I was using Google to print out the PDF generated and having this problem.
If I try Internet Explorer there is no problem.

PHP convert 24-bit colors to 4-bit

Background, I am converting images to ascii art. This works perfectly and even works with 24-bit color, converting the colors to the right rgb values. However, I now want to render the ascii art in 4-bit color palette rather than 24-bit.
How do I convert 24-bit colors to 4-bit with PHP?
More specifically, I have the standard IRC color pallet which I need to convert any given Hexidecimal or RGB value to. It is preferred that the colors match as best as possible when converted to the 4-bit color.
Other ideas I have had on this are to convert the image itself to a 4-bit palette (using GD, which is what I use to read in the colors right now) before trying to grab colors off of it. And another idea might be to define a color range for each of the following color and just check that the given 24-bit color is in the range, however I wouldn't know how to get the ranges for all colors into that palette.
imagetruecolortopalette allows you to reduce the colours, but results can vary wildly and I don't know if there is a way of 'mapping' the colours correctly or specifying the palette.
Test image (24-bit):
Reduced to 4-bit (without dithering):
$img = imagecreatefrompng('Bliss.png');
imagetruecolortopalette($img, false, 16);
imagepng($img, 'Bliss2.png');
Reduced to 4-bit (with dithering):
$img = imagecreatefrompng('Bliss.png');
imagetruecolortopalette($img, true, 16);
imagepng($img, 'Bliss3.png');
As you can see, results are far from perfect. But perhaps this is a good start for you.
In the end, despite the wonderful suggestions surrounding imagemagick I found a good solution using straight php. I was able to calculate the closest color through the use of delta E 2000 with a modified version of php-color-difference library found on github, here is my fork: https://github.com/nalipaz/php-color-difference
The pertinent example is:
<?php
include('lib/color_difference.class.php');
$palette = array(
'00' => array(255, 255, 255),
'01' => array(0, 0, 0),
'02' => array(0, 0, 139),
'03' => array(0, 128, 0),
'04' => array(255, 0, 0),
'05' => array(139, 0, 0),
'06' => array(128, 0, 128),
'07' => array(255, 165, 0),
'08' => array(255, 255, 0),
'09' => array(50, 205, 50),
'10' => array(0, 128, 128),
'11' => array(173, 216, 230),
'12' => array(0, 0, 255),
'13' => array(255, 105, 180),
'14' => array(128, 128, 128),
'15' => array(211, 211, 211),
);
$color_rgb = array(255, 255, 128);
$color_delta_e = new color_difference($color_rgb);
$match_index = $color_delta_e->getClosestMatch($palette);
$color = $palette[$match_index];
I am pretty happy with this solution and smaller amount of overhead. Thanks for the suggestions guys.
I think ImageMagick (or GraphicsMagick) can do this with the -depth option. There's a discussion of it here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=15395
UPDATE: I should add that ImageMagick is not a PHP library, however there's a PECL wrapper (imagick) for it at http://pecl.php.net/package/imagick.
I think you need to use remap to map the colours in the image to the palette of colours in your swatch. I do it at the command line like this:
convert image.jpg -remap palette.jpg out.jpg
You may, or may not want the dither option - check it out.
Original image is here:
and this is my palette.jpg (you only need a really small image, this is way too big - I will address this shortly)
and the result
You can also create your palette according to the colours you want using ImageMagick. I hand-coded the following and didn't pay too much attention, so you would want to check the RGB values in here before assuming they are correct:
#/bin/bash
cat<<EOF | convert txt:- palette.png
# ImageMagick pixel enumeration: 8,2,256,rgb
0,0: (255,255,255)
1,0: (0,0,0)
2,0: (0,0,255)
3,0: (255,255,0)
4,0: (255,0,0)
5,0: (128,128,128)
6,0: (255,105,180)
7,0: (173,216,230)
0,1: (50,205,50)
1,1: (139,0,0)
2,1: (255,165,0)
3,1: (128,0,128)
4,1: (0,0,139)
5,1: (0,128,128)
6,1: (0,128,0)
7,1: (211,211,211)
EOF
Basically, the script above gives ImageMagick the RGB values as text and asks it to make small 8x2 image that looks like this:
Then you would use this palette with your remap operation.

Categories