告诉abcPdf缩放HTML,以适应一个单一的PDF页面上 [英] Tell abcPdf to scale the html to fit on a single pdf page

查看:655
本文介绍了告诉abcPdf缩放HTML,以适应一个单一的PDF页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用abcPdf转换成HTML报表成PDF文件。的PDF必须是一个单个的风景A4纸

你知道,如果有任何方式告诉abcPdf扩展HTML页面以适应PDF单页上?我试着用<一个href="http://www.websupergoo.com/helppdf5net/source/5-abcpdf5/xtransform/1-methods/magnify.htm">Magnify()法和缩放时的内容,但还是把它分成页,即使它会适合在一个页面上。我一直在抓我的头在这一段时间,现在,我想知道是否有人在做这件事。

这里的code我使用的那一刻:

 公共字节[] UrlToPdf(字符串URL,PageOrientation PO)
{
    使用(文件theDoc =新的文档())
    {
        //当在横向模式下:
        //我们用两个变换围绕应用通用的90度旋转
        //在文档的中心和旋转相同的量绘制矩形。
        如果(PO == PageOrientation.Landscape)
        {
            //应用旋转变换
            双W = theDoc.MediaBox.Width;
            双H =​​ theDoc.MediaBox.Height;
            双L = theDoc.MediaBox.Left;
            双B = theDoc.MediaBox.Bottom;
            theDoc.Transform.Rotate(90,L,B);
            theDoc.Transform.Translate(瓦特,0);

            //旋转我们的矩形
            theDoc.Rect.Width = H;
            theDoc.Rect.Height = W;

            //要改变,我们需要一个旋转应用到根页对象的文档的默认方向。
            //这样做既保证了文档中的每个页面中查看旋转。
            INT theDocID = Convert.ToInt32(theDoc.GetInfo(theDoc.Root,页数));
            theDoc.SetInfo(theDocID,/旋转,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;

        //添加网址记录。
        INT theID = theDoc.AddImageUrl(URL,真实,0,真正的);
        而(真)
        {
            如果(!theDoc.Chainable(theID))
                打破;
            theDoc.Page = theDoc.AddPage();
            theID = theDoc.AddImageToChain(theID);
        }
        //扁平化的页面(这意味着什么)
        的for(int i = 1; I&LT; = theDoc.PageCount;我++)
        {
            theDoc.PageNumber =我;
            theDoc.Flatten();
        }

        返回theDoc.GetData();
    }
}
 

解决方案

因此​​,这里是我如何解决这一点。

首先,我需要的HTML页面的高度将被传递到PDF生成方法,所以我说这个页面上,如PDF-ED:

 &LT; ASP:HiddenField ID =hfHeight=服务器/&GT;
 

和背后的code:

 保护无效Page_Init(对象发件人,EventArgs的)
{
    如果(!的IsPostBack)
    {
        字符串scriptKey =WidhtHeightForPdf;
        如果(!Page.ClientScript.IsClientScriptBlockRegistered(scriptKey))
        {
            StringBuilder的SB =新的StringBuilder();
            sb.AppendLine(&其中;脚本&gt;中)
                .AppendLine(的document.getElementById('+ hfHeight.ClientID +')。价值= document.body.clientHeight;)
                .AppendLine(&所述; /脚本&gt;中);

            Page.ClientScript.RegisterStartupScript(typeof运算(页),scriptKey,sb.ToString());
        }
    }
}
 

现在,当我打电话的PDF生成方法我可以通过它的HTML的高度。有一次,我有高度它计算的PDF视宽度的所有的事情,这样的高度在PDF页面上的配合:

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

,然后指定 theDoc 的BrowserWidth参数既可以通过 HtmlOptions

  theDoc.HtmlOptions.BrowserWidth = intHTMLWidth;
 

或添加的网址 theDoc 时:

  INT theID = theDoc.AddImageUrl(URL,真实,intHTMLWidth,真正的);
 

编辑:这解决了问题,所以我将其标记为一个答案。现在,接下来要做的事情就是创建一个基于HTML的宽度和高度的写真或横向模式的PDF格式,这样可以最大限度的空间使用的PDF页面上。

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();
    }
}

解决方案

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.

这篇关于告诉abcPdf缩放HTML,以适应一个单一的PDF页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆