I can't seem to fit a webpage that has a longer height inside my bounding box below. When the BrowserWidth is smaller, that webpage overflows the page, but when it's larger then the bounding box it fits fine.
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();
}
}
}
Fixed it with the following.
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);
}