views:

366

answers:

2

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 the HTML page to fit on a single page in the pdf? I tried using the Magnify() method, and it scales the content but still breaks it into pages, even though it would fit on one page. I've been scratching my head on this for a while now, and am wondering if anyone has done it.

Here's the code I'm using at the moment:

public byte[] UrlToPdf(string url, PageOrientation po)
{
    using (Doc theDoc = new Doc())
    {
        // When in landscape mode:
        // We use two transforms to apply a generic 90 degree rotation around
        // the center of the document and rotate the drawing rectangle by the same amount.
        if (po == PageOrientation.Landscape)
        {
            // apply a rotation transform
            double w = theDoc.MediaBox.Width;
            double h = theDoc.MediaBox.Height;
            double l = theDoc.MediaBox.Left;
            double b = theDoc.MediaBox.Bottom;
            theDoc.Transform.Rotate(90, l, b);
            theDoc.Transform.Translate(w, 0);

            // rotate our rectangle
            theDoc.Rect.Width = h;
            theDoc.Rect.Height = w;

            // To change the default orientation of the document we need to apply a rotation to the root page object.
            //By doing this we ensure that every page in the document is viewed rotated.
            int theDocID = Convert.ToInt32(theDoc.GetInfo(theDoc.Root, "Pages"));
            theDoc.SetInfo(theDocID, "/Rotate", "90");
        }

        theDoc.HtmlOptions.PageCacheEnabled = false;
        theDoc.HtmlOptions.AddForms = false;
        theDoc.HtmlOptions.AddLinks = false;
        theDoc.HtmlOptions.AddMovies = false;
        theDoc.HtmlOptions.FontEmbed = false;
        theDoc.HtmlOptions.UseResync = false;
        theDoc.HtmlOptions.UseVideo = false;
        theDoc.HtmlOptions.UseScript = false;
        theDoc.HtmlOptions.HideBackground = false;
        theDoc.HtmlOptions.Timeout = 60000;
        theDoc.HtmlOptions.BrowserWidth = 0;
        theDoc.HtmlOptions.ImageQuality = 101;

        // Add url to document.
        int theID = theDoc.AddImageUrl(url, true, 0, true);
        while (true)
        {
            if (!theDoc.Chainable(theID))
                break;
            theDoc.Page = theDoc.AddPage();
            theID = theDoc.AddImageToChain(theID);
        }
        //Flattening the pages (Whatever that means)
        for (int i = 1; i <= theDoc.PageCount; i++)
        {
            theDoc.PageNumber = i;
            theDoc.Flatten();
        }

        return theDoc.GetData();
    }
}
A: 

This can help you out.
http://www.websupergoo.com/helppdf7net/default.html?page=source%2F3-concepts%2Fg-htmlrender.htm

zapping
Thanks, obviously I've read this page already, but your answer gave me the push I needed to give this another try.I don't think abcPdf has such a feature built in, so what I am going to do is get the size of the html page and resize it so that it fits on a single pdf page. Will post here when I get some results.Thanks.
beon
+1  A: 

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:

<asp:HiddenField ID="hfHeight" runat="server" />

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("<script>")
                .AppendLine("document.getElementById('" + hfHeight.ClientID + "').value = document.body.clientHeight;")
                .AppendLine("</script>");

            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.

beon