Welcome Guest Search | Active Topics | Sign In | Register

HtmlToPdf: GeneratePageImages Options
glinkot
Posted: Wednesday, January 18, 2012 12:32:30 AM
Rank: Member
Groups: Member

Joined: 1/11/2012
Posts: 27
Hi,

I have a chart that renders in HTML using javascript. The chart is of a defined size and sits as an image on the page, by itself in the top left corner.

Let's say the chart is now rendered, and it's there as an 800x600 image on the top left of my page (or in a HTML file that references it). I see the 'generatepageimages' property but can't see a sample, or how you might define the dimensions/file format of it for example.

How would I save that page as an 800x600 png file, for example?

Many thanks!

Mark
eo_support
Posted: Wednesday, January 18, 2012 9:20:39 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
glinkot
Posted: Wednesday, January 18, 2012 10:11:42 PM
Rank: Member
Groups: Member

Joined: 1/11/2012
Posts: 27
Thanks. I'd seen that part, but I wasn't sure of the best way to size the image to (800x600) or whatever. Right now I use something like the below:

EO.Pdf.HtmlToPdf.Options.PageSize = EO.Pdf.PdfPageSizes.A4;
EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0.5f, 0.5f, 7.0f, 10.75f);

How would I define a custom size of 800x600 (assuming I only want to capture the top left region of my page)?

Thanks

eo_support
Posted: Thursday, January 19, 2012 9:30:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

The following rules apply with GeneratePageImages:

1. The result image is always exactly the same as what the page would look in a PDF Viewer. So for example, if you have a 0.5 inch left margin on your OutputArea, then the result image will have a 0.5 inch left margin. Try to think the result image as a "screenshot" of what the PDF file will look in a PDF viewer. This means if you do not want the page margins, then you can either set your OutputArea so that it does not have any margin; Or get the result image with margin first, then using GDI+ to trim the edge off your image;

2. You can not specify the result image's pixel size. You can only specify your paper's size in inch (by setting PageSize). The final image size is determined by your page size and your screen resolution (most of time 96 pixel per inch);

As such in your case, you can capture the full page first, then use System.Drawing.Graphics to get a small section of it. The code would be something like this:

Code: C#
//This is your final image
Bitmap bmp = new Bitmap(800, 600);

//Create a Graphics object to draw Pdf page images into your image
Graphics gfx = Graphics.FromImage(bmp);

//There are many different versions of of DrawImage functions. Some
//can specify both source rectangle and destination rectangle. Choose
//one that fits your need 
gfx.DrawImage(pdf_result_image, ....);


Hope this helps. Let us know if you still have any questions.

Thanks!
glinkot
Posted: Thursday, January 19, 2012 9:22:13 PM
Rank: Member
Groups: Member

Joined: 1/11/2012
Posts: 27
Ok, sounds good. So I guess if I calculate 800 / 96 and set it to that width with no margins, etc that might be a good start! I will give it a go and let you know if I have issues. Many thanks.

Does this mean the current user's screen DPI is determined by your tool before rendering? So for example, if I implemented it using the calculations above then someone had a different display density - would this then ruin the size of what gets captured? Is DPI adjustable in some way?
eo_support
Posted: Friday, January 20, 2012 9:21:42 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

The current user's screen DPI is not determined by us. It's a Windows setting that user can change. We will read that value with the following code before every conversion:

Code: C#
IntPtr hDC = WindowsAPI.GetDC(IntPtr.Zero);
Graphics gfx = Graphics.FromHdc(hDC);
float dpi = gfx.DpiX;
WindowsAPIs.ReleaseDC(hDC);


Note here GetDC/ReleaseDC are Windows APIs so you must declare them through DllImport first.

Thanks
glinkot
Posted: Friday, January 20, 2012 5:36:09 PM
Rank: Member
Groups: Member

Joined: 1/11/2012
Posts: 27
Hi. Sorry, when I said 'determine' I meant 'read' rather than actually changing it. This is interesting - I would imagine sometimes this could create unexpected behaviour/ PDF appearance if you deployed an app and they have a different dpi to what your dev machine did during testing. In my case, where I am generating the PDF for emailing, the host machine dpi shouldn't affect the output and would never be viewed on that machine.

Perhaps a simple dpi property could be created, which is used if present. If not defined, the above code would invoke so it wouldn't affect existing implementations.

Thanks for listening!
eo_support
Posted: Saturday, January 21, 2012 10:06:04 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

You can turn the automatic DPI adjusting logic off by setting this property to false:

http://www.essentialobjects.com/doc/4/eo.pdf.htmltopdfoptions.autoadjustfordpi.aspx

When that property is set to false, the converter will use fixed value 96.

Thanks!
glinkot
Posted: Saturday, January 21, 2012 11:42:20 PM
Rank: Member
Groups: Member

Joined: 1/11/2012
Posts: 27
Excellent! Many thanks :)


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.