PDF Watermark with: Transparency, underlined, bold and Italicized Text Ghostscript and Php - php

I am trying to watermark( with text ) pdf with ghostscript. Earlier, I added page numbers via ghostscript, even text based watermark in pdf but transparency and watermark above and below the content seem to be an issue and for that i have had failed attempts with Postscript's PDFMARK extension. Below is the ps code(In the body of EndPage), i have deliberately tried to write the text instead of Stroke in order to allow bold, italics and underlines. I am considering to change from php to any other language if it offers a free alternative to this.
/Helvetica 44 selectfont
(Shadab Hussain Watermark on Test)
dup
stringwidth pop
currentpagedevice /PageSize get 0 get
exch sub 2 div
currentpagedevice /PageSize get 1 get 2 div moveto
show

You can't do transparency in PostScript, because it isn't supported in the graphics model. This is one of the major differences between PostScript and PDF.
Ghostscript does have some extensions to the PostScript imaging model which will allow some use of PDF transparency from PostScript, but it's non-trivial. You should (using the latest HEAD from the Ghostscript Git repository) look at ghostpdl/examples/transparency_example.ps for an example of using transparency from PostScript. Note that these extensions are totally Ghostscript-specific and will not work with any other PostScript interpreter.
As regards 'above' and 'below' the content. You either execute the PostScript program on BeginPage, in which case it is laid down before the PDF content, and may be overwritten by it (hint: many PDF files start by drawing a white rectangle covering the page), or you execute the PostScript program on EndPage (both is also possible), in which case it overwrites the already drawn content.

Related

Creating high quality images for printing (CMYK) on websdrver

I'm struggling with generating high-quality images for printing - mainly because there is really poor support for CMYK colors in PHP libraries and Linux CLI tools.
The situation is as follows. I have input materials in tiff format and CMYK colors - it is the background. I need to add some text to it and save as tiff with CMYK colors. The challenge is to use exact CMYK colors (so no temporary export/import to sRGB during the process is possible).
My first thought was to use Imagemagick but Imageimagick has no option to draw in CMYK. I need to work in sRGB and export material to CMYK. So some information about colors are changed during the process.
It seems this is not possible with PHP to add text to image without temporarily converting colors to sRGB, so I'm looking for any option like Linux CLI tool, node.js lib, etc. The goal is to at least generate text as an image with a transparent background and with colors defined in CMYK with exact values or do the same directly on the background image.
Finally, I gave up with Imagemagick. As we can use PDF for professional printing I decided to go this way.
I installed TCPDF and TCPDI. With TCPDI I imported previously prepared pdf template with graphics etc. Using TCPDF I added necessary dynamic text - TCPDF support CMYK with no issues.
Depending on where you print you can embed fonts to PDF (it can be done with TCPDF) or change texts to outlines which is a small challenge. I needed to change fonts to outlines (client request) so I did it with Ghostscript.
It took me a whole day to figure it out, so I post it in case somebody will have a similar problem. Definitely not an answer to the question, but at least a good solution for the problem itself.

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

pdf to png or jpg conversion and back again with php - losing fonts

I need to accept hundreds of pdfs at a time via PHP. I am storing these files on S3, so, file size will become a concern - not only for storing, but general handling. I'm finding the best way to reduce file size is conversion from PDF to PNG and back to PDF. A 15M file drops to 700kb. The problem is I'm losing certain fonts. Is there a way to ensure this doesn't happen? How do I ensure the process I use maintains the fonts in the original document? Is there some massive font library I can install?
from the command line I've tried...
Imagemagick
Ghostscript
pdftk
inkscape (real nice output)
They all work with varying levels of success, but each of them lose certain fonts - and not always the same ones.
Nope!
The .PDF format is an encapsulation of "graphics commands," such as "render the following text at position (X,Y) in the workspace using font Z."
When you "convert" such a file to any(!) "image file" format, you are in fact asking the PDF-engine to "carry out those graphics commands," producing a bitmap (a rectangular grid of pixels ...) as its only output.
Well, once you have done that, "you can never go back." The PDF-engine rendered its rectangular grid of pixels as best it could, and now, both it and the PDF-file that it consumed are gone, leaving you only with a rectangular grid of (output) pixels.

Is there a PHP library for drawing text as a vector outline?

In PHP, it's quite easy to use GD to draw text onto a raster canvas in a certain font (according to a TTF file held on the server), and output it as a PNG or JPEG.
I want to do roughly the same thing, but to draw the text as a vector outline and output it as an SVG.
(I do not want to embed the font itself in the resulting SVG, as the font is not licensed for that.)
Is there any PHP library that provides this sort of functionality?
It's possible to script inkscape to do this.
The following command opens "example.svg" and selects all shapes in the file and converts them to paths, and then saves and closes the file.
inkscape --verb EditSelectAll --verb ObjectToPath --verb FileSave --verb FileClose example.svg
I was able to find CleverSVG trough searching, however I did not try this myself, and I am unsure if it will be able to draw text without forwarding the font.

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