drawing alpha transparent rectangles to PDF from PHP - php

I am currently using FPDF to generate PDF files. What I would need is to draw alpha-transparent rectangles (say with opacity 0.5) on top of an image.
I cannot find how to do this in FPDF, since SetDrawColor and SetFillColor (which are used to draw the rectangles) only take RGB arguments, no alpha value. So they automatically completely cover images or lines placed there with Image or Line.
How can I generate PDF files from PHP that have semi-transparent colored rectangles on top of a given background image and/or on top of a given set of (dashed/full) lines?

I suddenly bumped into the answer: apparently, transparency is not stored on the color level in PDFs, but on the object level. And this add-on provides functionality for it: http://fpdf.org/en/script/script74.php

Related

How do CMYK/RGB color spaces work in pdfs and images, and how does it affect their inter convertibility?

I have a task where I need to take PDFs that are mock ups of printing products, and check their resolution, size and colour-space. I need to use Imagick with PHP to complete this task.
The printing shop that will print these PDFs only have CMYK printers and so, the uploaded PDF need to have CMYK colours. But I am not clear on how colour-spaces(CMYK/RGB) work in PDF, or in jpeg/png images. So, I have a few questions that will hopefully help me understand the thing better and complete the task:
From what I understand, we can draw objects or add images to the pdf that can have their colours defined as RGB or CMYK, but how does this affect the colour-space of the entire PDF?
Is it possible to check the colour-space of a PDF in php, without converting it jpeg/png?
If I have images in a PDF defined in either CMYK or RGB colour-space and convert the PDF to jpeg/png with Imagick, does the colour-space remain the same in the converted image unless specifically mentioned by Imagick::transformImageColorspace()?
A short background information on how colour-spaces work, how they are defined and detected and how they are affected when the file is converted from one mime-type to another.
P.S.: I am converting the PDFs to jpeg/png and checking the colour of the converted file as below, but it always gives false, no matter what pdf I use.
$img = new imagick(self::$_imgArray[0]);
if($img->getimagecolorspace() == imagick::COLORSPACE_CMYK)
echo "Image is in CMYK";
I have a task where I need to take PDFs that are mock ups of printing products, and check their resolution, size and colour-space.
A PDF page does not have a resolution (though images on the page do). It does have a "physical" dimension, default being Letter size. PDF units are by default 1/72 inch. If a PDF page contains pure vector data, then it look great at any resolution.
See below for more detail, but a single PDF page/document can contain one or more of Gray, RGB, CMYK, LAB, and more, color spaces.
but how does this affect the colour-space of the entire PDF?
It doesn't, the PDF itself does not have an overall color space. Typically a PDF processor would convert all graphics to a target color space, e.g. Chrome would at some point have everything in RGB since it is drawing to a screen.
Is it possible to check the colour-space of a PDF in php, without converting it jpeg/png?
Sure, though a single PDF could contain greyscale, rgb, cmyk, lab, separation colors, etc. Again, there is no one color space in a PDF file.
If I have images in a PDF defined in either CMYK or RGB colour-space and convert the PDF to jpeg/png with Imagick, does the
colour-space remain the same in the converted image unless
specifically mentioned by Imagick::transformImageColorspace()?
It would depend on the software doing the conversion. Since PNG does not support CMYK, then at the very least any CMYK would be converted. Exactly what happens depends on the software, the settings, and the target output format and what is supports.
A short background information on how colour-spaces work, how they are defined and detected and how they are affected when the file is
converted from one mime-type to another.
See section 8.6 here: https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf
Here is another good link
https://www.color-management-guide.com/color-spaces.html

Convert PDF portrait to image landscape using Imagemagick and PHP

I want to convert a multipages PDF to a series of png files, while the PDF is portrait and png file is landscape, using imagemagick. I have achieve the conversion from PDF portrait to png protrait(code attached below). But have no idea how to make portrait image to landsacpe with no distortion, (not rotate the image).
Can anyone kindly help me? Thanks.
$infile=$direcory."/Test.pdf";
$images=new Imagick();
$bodercolor=new ImagickPixel("white");
$images->setResolution(220, 220);
$images->readimage($infile);
$images->setimageformat("png");
foreach ($images as $i=>$image){
//set backgroud color = white
$image->setimagebordercolor($bodercolor);
$image->borderimage($bodercolor, 0, 0);
$image->writeimage($direcory."/test-pg".$i.".png");
}
$images->clear();
$images->destroy();
Actually, what I want is rotate the vertical page to horizontal with the objects on it have almost no changes. Like what we are able to do using Microsoft Word/PowerPoint when you change page orientation to landscape.
When you change a slide's orientation in PowerPoint, it recomputes the whole layout: centered elements are shifted left or right based on the new width, multi-line text is re-wrapped, and so on. This is possible because PowerPoint knows how the elements are supposed to be formatted.
You can't do that with a PDF file because it doesn't contain that kind of high-level formatting information; a PDF is basically just a description of where to put ink on paper. Reading a PDF file, you don't know that a particular line of text is supposed to be centered; you just know the (X, Y) coordinates where its top-left corner should be positioned. If one line of text appears below another, there's nothing that tells you whether words at the beginning of the second line should be moved to the end of the first when the page gets wider (e.g. word-wrapping) or if they're unrelated items (like separate bullet points) that should not be combined.
Fundamentally, a PDF is more like a picture than an editable document.
Well, your task then seems to be two steps: gettng png from pdf (which you say you have done) and getting png image to change sizes.
Question yourself what do you want to do - rotate the vertical image to be horizontal? Crop some part of the vertical image and fit it into horizontal space?
What you need to do is to decide the answer to the above question. After that - take a pen, piece of paper and draw the process. Draw the vertical rectangle, then fill in some area that is you content on that page, then rotate/transform/resize/fit and etc - whatever your answer was that content area into horizontal rectangle.
After that you can write your code - deal with pixel width/height, rotation angle, fitting the area to the horizontal space and etc.

Scale HTML proportionally to fit exactly to PDF A4 size

I am using PHP, Mysql, jQuery. I have a webpage that is to be converted into high-res A4 size PDF: http://optisolbusiness.com/funeral_site/sample/index/id/255.
I have converted the HTML to PDF using wkhtmltopdf, which works great.
Here is the generated PDF http://optisolbusiness.com/Guru/Gurupdf/optisol.pdf. But the HTML is not fitting exactly to PDF size; There are spaces around the HTML in PDF. How to scale HTML to fit A4 PDF size 100%? Importantly the content inside the html (ie) text size, images width and height, background images also to be scaled proportionally.
Your background image is not exactly high-res, this won't look great in print.
I don't know wkhtmltopdf itself, but your body already has absolute dimensions set (in inches). This is probably the problem. Your body has a max size, the content has an absolute size too (given due to the background image pixel dimensions).
This is not a good starting point for html-to-print transformaions, and PDF is essentially print.
what to do (intermediate)
remove any size restrictions from body
wkhtml... has a switch called zoom, 1.5 should be an appropriate value to fill the page
use page-size a4
what to do (the "right" way)
remove size restrictions from body
build the background borders (the black ones) with html elements and css styling
refrain from defining "width" rules for those. You will only have to define a "width" once, all other widths should be set to "auto".
heights will prove troublesome, because divs are only as high as their content requires. But setting height: 100% does not respect border and margin sizes.
that yellow cross could be designed in css too, or a much higher resolution png/jpeg
Use only "real" dimensions. That means do not use pixels, use points, inches, or mm. You can use % values, but make sure those are % values of real dimensions (that means that at some point a parent element has a real dimension)
I'd say that you're always going to struggle to get this to be perfect. In my opinion you're better off writing the PDF directly rather than relying on a third party tool.
Consider looking into FPDF, an open-source PHP PDF writing library. Be warned, the website looks out of date, but the functionality works beautifully.
You can set the size of the body to the size of the page.. in case of A4 that is 210x297mm.
Btw: you should only be using width in percentage, otherwise wkhtmltopdf will have to try and convert it.
So make sure you use width: 100%; if you want it to fill all the room. ;)
BTW: If you want real high quality PDFs you will need to create them conforming to at least the PDF/X-3 standard. I don't think wkhtmltopdf does that tho.

PHP ImageMagick making images transparent and drawing them ontop of other images

I am trying to make a program that can export images with another image(that is semi transparent) drawn on top of it. I'd like to be able to control the transparency on a per pixel value for the top image; and do it fairly quickly. Is there something built into ImageMagick to do this? Both of the images will have full opacity when loaded since they are all jpegs.
I guess you could think of it as a kind of watermark, although it would be procedural.
You can use Imagick::setImageOpacity to set the opacity of the watermark first.
Then, use Imagick::compositeImage to apply the watermark image on top of your base.

Finding blank areas of an image file using ImageMagick

I need to be able to determine if a part of an image (outside of predetermined crop marks) contains any image content.
Is there a way with ImageMagick (specifically the php interface) to do this?
Scenario:
The canvas is 8.5x11 with .5 in margins on the top, left, and bottom edges and a 1 in margin on the right edge. The image needs to fit within the crop marks for printing.
Normally I use Photoshop actions to do this, but I am trying to automate the process.
Replace everything within crop marks with black/white rectangle, do a histogram of the resulting image and analyze it?
I can do this with command-line version of ImageMagick, but dont know how to express it with PHP api.

Categories