Tell abcPdf to scale the html to fit on a single pdf page

前端 未结 3 1569
孤街浪徒
孤街浪徒 2021-01-03 01:27

I am using abcPdf to convert an HTML report into a pdf file. The pdf has to be a single landscape A4 page.

Do you know if there is any way to tell abcPdf to scale th

3条回答
  •  难免孤独
    2021-01-03 02:18

    So here's how I solved this.

    First of all, I needed the height of the HTML page to be passed to the pdf generating method, so I added this on the page to be pdf-ed:

    
    

    and in the code behind:

    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string scriptKey = "WidhtHeightForPdf";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(scriptKey))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("");
    
                Page.ClientScript.RegisterStartupScript(typeof(Page), scriptKey, sb.ToString());
            }
        }
    }
    

    Now, when I call the pdf generating method I can pass it the height of the HTML. Once I have the height it's all a matter of calculating the width of the pdf "viewport" such that the height fits on the pdf page:

    int intHTMLWidth = height.Value * Convert.ToInt32(theDoc.Rect.Width / theDoc.Rect.Height);
    

    And then specify the BrowserWidth parameter either through HtmlOptions of theDoc:

    theDoc.HtmlOptions.BrowserWidth = intHTMLWidth;
    

    or when adding the url to theDoc:

    int theID = theDoc.AddImageUrl(url, true, intHTMLWidth, true);
    

    EDIT: This solves the question, so I'm going to mark it as an answer. Now the next thing to do is create the pdf in protrait or landscape mode based on the width and height of the HTML, so that maximum space is used on the pdf page.

提交回复
热议问题