Forum How do I...?

Print directly to jpg or png

afingret
Is it possible to render a jpg or png directly from Prince? Currently we are using ImageMagick to convert Prince generated PDF's to jpgs. (for web preview only) Effective, but slow where high-quality graphics come into play.
mikeday
Not yet; this is a feature we plan to add in the future though.
afingret
That's good news Mike. Any thoughts on your timeline? I won't hold you to it, but it would help us structure our own development.

Also, am I correct to believe that this would be significantly faster than our ImageMagick approach? Theoretically?
mikeday
It's difficult to put a timeframe on it at the moment. Our current focus is on OpenType font support, but we might be able to add image output support after that is complete. Adding rudimentary image output support is relatively straightforward, but getting it polished and supporting all Prince features well will take longer. In theory, image output directly from Prince should be faster than generating a PDF file and then converting that file to an image. How important is performance to your use case?
afingret
OpenType. Cool.

For image output, an accurate visual render is pretty important. It should "look" like the pdf. Also, we'd like to be able to control the physical size of the output image - for thumbnails and previews and things.

Speed of course, is the reason for my question in the first place. For our use case, the faster the better if I'm being honest, but any appreciable speed gain would be welcome.
afingret
Any recent thoughts on image output support? Just wondering.
mikeday
No progress on this issue yet, sorry. Have you tried pdftoppm from the xpdf package?
afingret
No, we are using ImageMagick. Any reason to use pdftoppm instead? I'm not a programmer, so please excuse my limited understanding of these things.
mikeday
I thought it might be faster, but speed testing here is not showing any significant difference, so perhaps it isn't.
Dethe
My company is also very interested in outputting directly to PNG for web previews, and speed is important to us. We're evaluating tools for building our PDFs now, and Prince is ahead of the competition in all respects but this. Any word on support for image output would be very helpful.

Thanks!

--Dethe
mikeday
Have you tried ImageMagick or pdftoppm for converting PDF files to images?
Dethe
We can use ImageMagick (which in turn uses Ghostscript) as a work-around. There are a few reasons we'd like to do it with one tool.

1. ImageMagick + Ghostscript add a big chunk of memory overhead and additional dependencies.

2. Speed. We need to generate lots of previews and only need the actual PDFs at the final stage, so most PDFs will be converted to PNGs and thrown away. We'd rather just create the PNG, but need it to be an accurate representation of the final PDF.

3. Customization. For thumbnails, we want to drop some detail (like greeking small text). Again, not going through the intermediate disposable PDF would be nice, but we'd like to go through one tool to keep the look consistent (and the code base manageable).

Also, and this may not be as big an issue as it once was, but I'll have to see: Ghostscript produced noticeably poorer results with artifacts when converting. It also requires a different song-and-dance to get it to recognize fonts, IIRC.

You mentioned in this thread (back in 2008) that it was on the todo list, and image output is listed on your roadmap still. We can probably work around the issue for now, but I'm curious if it is on the near-term roadmap, or much further out.

For now, I will make a prototype of our existing tool using Prince instead of our in-house PDF/image generator to see how it goes and what true impact of using ImageMagick (or ppftoppm) is. Thanks for getting back to me so promptly.
crazyguymgd
Hey mikeday I was wondering if this feature is still on the Roadmap or if you would still recommend using something like ImageMagick on the side. It'd be nice to be able to use prince to go directly to PNG.

Thanks!
Matt
mikeday
I would suggest trying pdftoppm (based on xpdf) before ImageMagick/ghostscript, I think it will be faster.
crazyguymgd
Thanks Mike.
jamez
Hi there - any news on this feature? Is it going to be implemented any time soon?
Thanks!

- jamez
mikeday
Have you tried the xpdf/pdftoppm approach? Because that works right now. :)
jamez
Yes Mike, I tried it and it works well. I was just wondering if I could have skipped that step at this point. Never mind! :-)
conner_bw
Hello.

I'm working on PDF to JPEG conversion using Imagemagick and I'm having an issue with fonts.

A 6x9in PDF created by Prince will set page size (as seen in `pdfinfo`) to "Page size: 432 x 648 pts"

When I convert to JPG using Imagemagick with the bare minimum flags the JPG conversion size is 432 x 648 px.

!But! I want a JPEG of 2500 x 3750 px.

+ When I tweak Imagemagick to get a bigger image, the fonts get very jagged & ugly.

+ When I tweak Prince CSS in an attempt to "oversample" the source PDF I lose control over font sizes; they become very tiny.


Question:
In Prince, is there a way I can increase the Page Size PTS while still maintaining the relative font sizes?

Thank you for your help.


mikeday
What flags are you passing to ImageMagick? It should be possible to specify the DPI that you want.

In upcoming builds of Prince we will provide an option to render directly to an image file.
conner_bw
It's OK. I got decent results with `pdftoppm` instead.

Was trying:

    convert  /path/to/pdf -resize  2500x3750 /path/to/jpg


I was also messing around with variations of:

     convert  /path/to/pdf  -density 96 -resize 2500x3750 -define jpeg:extent=2MB /path/to/jpg


But it wasn't very good.
thebigstep
mikeday you mentioned that: "In upcoming builds of Prince we will provide an option to render directly to an image file". Any details when can we expect this much needed feature?
mikeday
Yes, it is available in the latest builds we have just released! :D

Raster Output Options:
  --raster-output=TEMPLATE  Enable raster output.
  --raster-format=FORMAT    Set raster output format (auto, png, jpeg).
  --raster-pages=RANGE      Set range of pages to rasterise (all, first or NUM).
  --raster-dpi=DPI          Set resolution of raster output.


The --raster-output option can take a template like "image%d.png" to create image1.png, image2.png, etc. or just a regular filename if you only want to rasterise the first page of the PDF.
daneren2005
I have started using the raster options and so far they seem like they work wonderfully, thank you for this! I was trying to use ImageMagick before but was continually unable to get it to produce good quality. I had it so it was mostly working but with gradiant backgrounds there was still banding even if I turned DPI up to 1000 and the color profiles were never quite correct. Your raster seems to do everything wonderfully without me messing with any complicated options.

I have a question on format=auto. What is the criteria for auto? For example right now I have users who are asking for the option for png output if they have a background with transparency in it. I am toying with just letting them choose between jpg and png, but it would be awesome if auto would detect if there is any spot on the image which is transparent (ie: not just detect if any image in the html has transparency). I somehow doubt that is what it is doing but I figured I would ask just in case.
mikeday
The default --raster-format=auto will just look at the specified file extension to determine the image format.

At the moment pages are rasterised on a solid white background, so there will be no actual transparency in the output regardless of the image format. Perhaps we could revisit this next year. :)
nsnsolutions
Hey Mike, do you have an sample code of how to render to jpeg or png from C# code.... I am currently following your C# samples and they work fine for PDF. I downloaded the lastest build and Dotnet dll.

Thanks

Dave from NSN

mikeday
We have not updated the C# wrapper to have special methods for this yet, but you can add your own command-line options via the SetOptions method.
nsnsolutions
Hey Mike, the goal for my company is to create a quick preview from an html template. I am playing around with the raster options to to create that using C# using the setOptions like you suggested, I'm fairly new to anything but the standard convert so there must be something fundamentally wrong with the way I am calling this.

Prince p = new Prine(EnginePath);
p.SetLog(LogPath);
p.SetOptions("--raster-output=Test.html");
p.SetOptions("--raster-format=jpeg");
p.SetOptions("--raster-pages=all");
p.SetOptions("--raster-dpi=2500X3650");
p.Convert("Test.html", "Test.jpeg");

Thanks

Dave


xeno.cmils
Scratch that Mike, I figured it out!

Here is a C# section that converts an html into a jpg

class Program
{
static void Main(string[] args)
{
convertToJPG();
}


static void convertToJPG()
{
Prince prn = new Prince("C:\\prince-newest-build\\bin\\prince.exe");
prn.SetLog("C:\\docs\\log.txt");

prn.SetOptions("--raster-output=C:\\Users\\ExampleUser\\Desktop\\ExampleFolder\\OutputImageName.jpeg --raster-format=jpeg --raster-pages=first --raster-dpi=96");

prn.Convert("C:\\Users\\ExampleUser\\Desktop\\ExampleFolder\\HTMLDocToMakeImage.html");
}
}
mikeday
Yes that is correct you got there before me. :)
daneren2005
I have a customer asking me about the color profiles on the resulting jpg. Can we have the output have an embedded color profile (or have it optional to embed a sRGB color profile)?
mikeday
We will investigate this.
daneren2005
Did you guys ever get a chance to look into the color profile for direct jpeg output?
mikeday
Not yet, we have been focused on SVG filters.
daneren2005
Can you add it to the TODO list to have the `raster-format` of `pdf`? The reason being is some printers seem to have issues with printing pdf's which come from Prince. Flattening that into a rasterized pdf should take care of it so they can still print an entire project at once, but not have to worry about how the specific printer is going to interpret the PDF. This is not incredibly time-sensitive since people can work around it by grabbing the rasterized jpg's and embedding them in a pdf, but it would be nice to have.
mikeday
You can do this by applying a filter to the root element of the document, which will force rasterisation of each page. For example:
:root { filter: brightness(1) }

Also the latest build of Prince tags all rasterised images as using the sRGB color profile.
daneren2005
I upgraded to the latest version, then tried adding that. It not only didn't rasterize the entire page (all the text is still selectable as individual objects), but it also just made everything blurry without any way (that I can tell) to get full quality back.

PS I did notice that some text SVG stuff which was broken (ie: drop-shadow) was fixed in the latest release. Props for that and embedding the sRGB profiles!
mikeday
The text is rendered as invisible objects to allow searching/selection in PDF viewers. The blurriness is because it's rasterised at 72dpi by default, you can increase this with:
@prince-pdf {
    prince-filter-resolution: 300dpi
}
daneren2005
Adding a filter-resolution did fix the blurriness. Oddly enough I am finding that it also shifts things around. I am seeing it move text up and some images got smaller by about 5%. The top level rasterize to jpeg option seems to be spot on still.
mikeday
This shifting around should be fixed in Prince 11.