Welcome Guest Search | Active Topics | Sign In | Register

MVC to PDF Options
KevinM
Posted: Wednesday, September 4, 2013 5:38:12 AM
Rank: Newbie
Groups: Member

Joined: 9/4/2013
Posts: 8
EO.PDF 2013
Visual Studio 2010 SP1
Windows 7 SP1

We have been trying to use your MVC to PDF functionality so far without success.

First approach


We tried

[RenderAsPDF(AutoConvert=false)]

//Trigger the conversion only when m.ExportToPDF is true
if (m.ExportToPDF)
{
string filename = string.Format("{0} {1}", brandTitle, model.Subtitle);
MVCToPDF.ResultFileName = filename;
MVCToPDF.RenderAsPDF();
}

First problem

We have some custom icons that produce this error

System.Exception was unhandled
HResult=-2146233088
Message=The font file 'icomoon' does not have 'glyf' or 'loca' table. Please check whether the file is a true type font file. EO.Pdf does not support post script font file.
Source=EO.Pdf
StackTrace:
at EO.Pdf.Internal.lr.a(Object A_0, ThreadExceptionEventArgs A_1)
at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

We have temporarily set them to Arial but then...

Second problem is that we are using

Cassette | Asset bundling for .NET web apps
http://getcassette.net/

In the Web.Config file we require this setting

<cassette rewriteHtml="true" />
http://getcassette.net/documentation/v2/web-config

"When true (the default), Cassette will rewrite HTML page output to allow bundles to be referenced in the <head> even after it has been rendered."

However, this does not play nicely with MVCToPDF and we lose all our styling. Setting it to false gets round this issue but is not ideal. Is there a way of getting your tool to play nicely with Cassette?

Second approach


We tried HtmlToPdf.ConvertHtml()

// Grab the html from the view
string html = RenderViewToString(this, "Area", model);

The above call is

private string RenderViewToString(Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, "_Layout");
ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);

return sw.ToString();
}
}

//Set the response header
HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.ContentType = "application/pdf";

//Convert to the output stream
HtmlToPdf.ConvertHtml(html, response.OutputStream);

response.End();



However, in this case, we again lose all our styling. i asume we should really be using ConvertUrl here but the url is either localhost in dev or an authenticated url.
eo_support
Posted: Wednesday, September 4, 2013 9:10:52 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,195
Hi,

You can use ConvertHrml, but you must set HtmlToPdf.Options.BaseUrl. Without BaseUrl the converter wouldn't know how to resolve the style Url (or any other relative Url in your HTML).

Thanks!
KevinM
Posted: Thursday, September 5, 2013 8:44:20 AM
Rank: Newbie
Groups: Member

Joined: 9/4/2013
Posts: 8
Thanks, I have in the meantime gotten this to work using the recommended MVCToPDF approach. The only outstanding issue is the Cassette rewriteHtml. However, so far, we've not had a problem with just setting it to false.

Another minor issue is that if you have a character such as Š (as in ŠKODA) in MVCToPDF.ResultFileName it translates it as `KODA. But, e.g., PdfDocument.Save(filename) handles this fine.
eo_support
Posted: Thursday, September 5, 2013 8:57:21 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,195
Hi,

Glad to hear that you got it working. Yes. We are aware of the file name issue. That has to do with the browser. What we do is to pass the name to the browser, and the browser has its own logic to prompt a "suggested save as file name" based on the name we pass to it. In fact different browser can give different names. So you may not want to use those "special" characters in your file name.

Please feel free to let us know if you have any more questions.

Thanks!
KevinM
Posted: Thursday, September 5, 2013 9:04:48 AM
Rank: Newbie
Groups: Member

Joined: 9/4/2013
Posts: 8
Yes, for now we've just avoided the Š for this case. But it's interesting that your PdfDocument.Save is fine (in Firefox at least, I didn't try the other browsers for that case) but not MVCToPDF.ResultFileName. I guess something different is going on behind the scenes.
eo_support
Posted: Thursday, September 5, 2013 9:11:41 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,195
Hi,

PdfDocument.Save pass the name to the server's file system directly and the file system can handle such file names. ResultFileName pass the name to the client browser (from the server) through HTTP and different browser handles this differently. So ResultFileName involves HTTP and your browser, while PdfDocument.Save does not.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.