HTML-Renderer icon indicating copy to clipboard operation
HTML-Renderer copied to clipboard

PdfGenerator scales by 133% (or 1/3)

Open jishcat opened this issue 8 years ago • 1 comments

I'm having issues with getting a page designed to fit on 8.5x11 Letter paper to size/position properly. So I just do a simple html with a div with a set height and width as a sanity check. First I did "width: 1in; height: 2in;" and the resulting div on the PDF was 1.33333 inches x 2.66666 inches. So I tried points of 72pt x 144pt, and the same thing. If I do 54pt x 108pt I get a box that is 1inx2in. So I have to specify sized that are 3/4 the size I really want in order to get the actual on page size I'm looking for apparently.

Is there some scaling going on that you can control? Do I need to do some reset on the body? What is the user.css that it uses. I wasn't able to find these things in the source code.

Here is the simple html I'm testing:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html><head></head><body style="padding:0;margin:0;"><div style="height: 108pt; width: 54pt; background-color: #ff0000; border: 3pt solid #0000ff;"></div></body></html>

Anyone have any thoughts on this?

jishcat avatar Aug 08 '17 03:08 jishcat

Might be a little late for your purposes, but for anyone's future reference the issue is in the ParseLength method of the CssValueParser. The pdf generation library is using points for sizing while the screen rendering is using pixels so all units are converted to pixels. There are 96 pixels per inch assuming 96dpi and there are 72 points per inch. 96/72 = 1.33

The solution would be to add context-dependent unit conversions, but for our project's needs we only cared about PDF generation. I don't currently have the time to implement a good fix, but for anyone who wants a quick and dirty fix for PDF generation replace lines 223-227 in the CssValueParser.cs file with the following code.

case CssConstants.In:
    factor = 72f; 
    break;
case CssConstants.Pt:
    factor = 1f;

You will only be able to specify widths and heights in points or inches. Font sizes should be specified in points. Images will probably have their own sizing issues separate from this, but I haven't tracked that down yet.

ghost avatar Feb 03 '18 01:02 ghost