在AbcPDF的边框中安装网站

时间:2012-08-22 20:51:26

标签: pdf-generation abcpdf

我似乎无法在下面的边框内放置一个较长的网页。当BrowserWidth较小时,该网页会溢出页面,但是当它大于边界框时,它就适合了。

public abstract class HtmlToPdfModel : IPdfModel
    {
        public string Url { get; set; }
        public int PageWidth { get; set; } // 850 (in pixels)
        public int PageHeight { get; set; } // 844 (in pixels)
        public string FileName { get; set; }

        public virtual byte[] GetStream()
        {
            using (var doc = new Doc())
            {
                var w = doc.MediaBox.Width;
                var h = doc.MediaBox.Height;
                var l = doc.MediaBox.Left;
                var b = doc.MediaBox.Bottom;

                doc.Transform.Rotate(90, l, b);
                doc.Transform.Translate(w, 0);
                doc.HtmlOptions.PageCacheEnabled = false;
                doc.Rect.Width = h; // 792
                doc.Rect.Height = w; // 612

                if (!string.IsNullOrWhiteSpace(string.Format("{0}&r={1}", Url, new Random().Next(1, 100000))))
                {
                    var ratio = GetViewPortRatio((int)doc.Rect.Width, (int)doc.Rect.Height, PageWidth, PageHeight);

                    doc.HtmlOptions.BrowserWidth = (int)(doc.Rect.Width * ratio);
                    doc.AddImageUrl(Url);
                }

                doc.SetInfo(doc.GetInfoInt(doc.Root, "Pages"), "/Rotate", "90");

                using (var ms = new MemoryStream())
                {
                    doc.Save(ms);
                    if (ms.CanSeek)
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                    }
                    return ms.GetBuffer();
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

修正了以下内容。

using (var doc = new Doc())
            {
                var w = doc.MediaBox.Width;
                var h = doc.MediaBox.Height;
                var l = doc.MediaBox.Left;
                var b = doc.MediaBox.Bottom;

                doc.Transform.Rotate(90, l, b);
                doc.Transform.Translate(w, 0);
                doc.HtmlOptions.PageCacheEnabled = false;
                doc.HtmlOptions.Engine = EngineType.Gecko;
                doc.Rect.Width = h;
                doc.Rect.Height = w;
                //doc.Rendering.DotsPerInch = 92;

                if (!string.IsNullOrWhiteSpace(string.Format("{0}&r={1}", Url, new Random().Next(1, 100000))))
                {
                    var actualWidth = PageWidth / .75f;
                    var dimensionsScale = GetViewPortScale(PageHeight, PageWidth);

                    doc.HtmlOptions.BrowserWidth = Convert.ToInt32(actualWidth * dimensionsScale);

                    doc.AddImageUrl(Url);
                }

                doc.SetInfo(doc.GetInfoInt(doc.Root, "Pages"), "/Rotate", "90");

                using (var ms = new MemoryStream())
                {
                    doc.Save(ms);
                    if (ms.CanSeek)
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                    }
                    return ms.GetBuffer();
                }
            }


       private float GetViewPortScale(float currentDimension, float dimension)
    {
        return (currentDimension / dimension);
    }