0

我似乎无法在下面的边界框内放置一个高度较长的网页。当 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();
                }
            }
        }
4

1 回答 1

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);
    }
于 2012-08-24T17:55:48.737 回答