andyhutch77 / MvcRazorToPdf

Create pdf documents within an asp .net mvc project by generating your views as normal but returning a PdfActionResult. This converts regular produced razor/html to pdf documents in the browser using the iTextXmlWorker.
125 stars 105 forks source link

Out of memory exception #8

Closed alexandis closed 9 years ago

alexandis commented 10 years ago

I am trying to output quite a small html. Anyway - it's output ok in html format. When trying MvcRazorToPdf - I am getting "Out of memory exception", here is the stack trace:

[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.] System.IO.MemoryStream.set_Capacity(Int32 value) +89 System.IO.MemoryStream.EnsureCapacity(Int32 value) +64 System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +330 iTextSharp.text.pdf.OutputStreamCounter.Write(Byte[] buffer, Int32 offset, Int32 count) +52 System.IO.Stream.WriteByte(Byte value) +48 iTextSharp.text.pdf.PdfDictionary.ToPdf(PdfWriter writer, Stream os) +64 iTextSharp.text.pdf.PdfDictionary.ToPdf(PdfWriter writer, Stream os) +238 iTextSharp.text.pdf.PdfIndirectObject.WriteTo(Stream os) +168 iTextSharp.text.pdf.PdfBody.Write(PdfIndirectObject indirect, Int32 refNumber, Int32 generation) +93 iTextSharp.text.pdf.PdfBody.Add(PdfObject objecta, Int32 refNumber, Int32 generation, Boolean inObjStm) +357 iTextSharp.text.pdf.PdfWriter.AddToBody(PdfObject objecta, PdfIndirectReference refa) +28 iTextSharp.text.pdf.PdfPages.AddPage(PdfDictionary page) +128 iTextSharp.text.pdf.PdfWriter.Add(PdfPage page, PdfContents contents) +259 iTextSharp.text.pdf.PdfDocument.NewPage() +1354 iTextSharp.text.pdf.PdfDocument.FlushFloatingElements() +638 iTextSharp.text.pdf.PdfDocument.NewPage() +33 iTextSharp.text.pdf.PdfDocument.Close() +169 iTextSharp.text.Document.Close() +110 MvcRazorToPdf.PdfActionResult.ExecuteResult(ControllerContext context) +870 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +52 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +54 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +54 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9688704 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Never worked with iTextSharp, don't have idea where to look at.

alexandis commented 10 years ago

These issues are related to css. It appears, that css support is quite poor here. It does not understand some tags / cannot calculate some values, throws exceptions and then somehow it causes overflow.

andyhutch77 commented 10 years ago

Hi Alexandis, sorry but I am really short on time at the moment and have been struggling to keep up with my github issues. I usually find debugging the razor by taking out elements one at a time helps me figure out the problem html element. I also paste the raw html into the demo on itext site because if that fails then the translation will fail within my package. http://demo.itextsupport.com/xmlworker/

andyhutch77 commented 10 years ago

I think you need to set the image provider, info here:

http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html#itextdoc-menu-10

andyhutch77 commented 10 years ago

Hi Alexandis, Yes, that makes sense.

You need to provide an image provider which basically defines the root of your web site.

This is the info you need here:

http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html#itextdoc-menu-10

Andy

On Monday, 18 August 2014, 10:13, alexandis notifications@github.com wrote:

Maybe the hint for you. The inner exception i discovered is "'iTextSharp.tool.xml.pipeline.html.NoImageProviderException" — Reply to this email directly or view it on GitHub.

alexandis commented 10 years ago

Hi, Andy, thanks for reply. The matter is I don't use images at all, so the exception surprised me :) Currently I sticked to simplifying my css at maximum to avoid problems (lack of time in the project). But if they say to beautify output - I will take a careful look at your links. Thanks for your work!

andyhutch77 commented 10 years ago

It could be related to another provider, image or resource.

Are you trying to resolve against a relative path?

On Monday, 18 August 2014, 11:24, alexandis notifications@github.com wrote:

Hi, Andy, thanks for reply. The matter is I don't use images at all, so the exception surprised me :) Currently I sticked to simplifying my css at maximum to avoid problems (lack of time in the project). But if they will say to beautify output - I will take a careful look at your links. Thanks for your work! — Reply to this email directly or view it on GitHub.

alexandis commented 10 years ago

No, it was a css, referenced via absolute url and no any urls inside... But it's not a problem now, I have simplified css. The problem now is that despite the fact css is very easy - PDF "understand" only a little part of it. I am lack of time seriously, but would like to look it more or less fitting the rest of design.

Text alignment in the table is ignored, header and cell colors are ignored, cell paddings are ignored, too. What do i do? Should i use inline styles??

andyhutch77 commented 10 years ago

I use css in the header, first I check that it formats in the itext demo.

That way I know it works beforehand.

It is possible to use a different renderer, I think there are other html to pdf convertors but it could be quite time consuming.

On Monday, 18 August 2014, 12:11, alexandis notifications@github.com wrote:

No, it was a css, referenced via absolute url and no any urls inside... But it's not a problem now, I have simplified css. The problem now is that despite the fact css is very easy - PDF "understand" only a little part of it. I am lack of time seriously, but would like to look it more or less fitting the rest of design. Text alignment in the table is ignored, header and cell colors are ignored, cell paddings are ignored, too. What do i do? Should i use inline styles?? — Reply to this email directly or view it on GitHub.

alexandis commented 10 years ago

I gave up and put everything as inline style, because including css directly into header did not help. Now everything looks more or less ok, but font size is uncontrolled :( I tried different units, but no matter. So my font is displayed bigger than required by default and some layout is broken because of it... How do you manage to make font size work?

andyhutch77 commented 9 years ago

Font sizes are controlled via css.