Welcome Guest Search | Active Topics | Sign In | Register

HtmlToPdf.Options.BeforeRenderPage Not firing Options
EssKay
Posted: Tuesday, June 18, 2019 1:04:00 PM
Rank: Newbie
Groups: Member

Joined: 6/18/2019
Posts: 4
Hi,
I'm using trial version 2019 . I have a requirement where we have different header for first page and different header from 2nd page onward.

So I'm trying to get the page number in the BeforeRenderPage event handler and change the headers accordingly based on the page index.

The code is like this,

[EO.Pdf.Mvc.RenderAsPDF(AutoConvert = false)]
public ActionResult CreatePDF()
{
HtmlToPdf.Options.BeforeRenderPage += this.On_BeforeRenderPage;

MVCToPDF.SendToClient = true;
MVCToPDF.ResultAsDownload = false;
MVCToPDF.ResultFileName = "TestFile.PDF";
MVCToPDF.RenderAsPDF(AfterRender);

return View();
}

private void On_BeforeRenderPage(object sender, PdfPageEventArgs e)
{
string headerStr=string.empty;
if(e.Page.Index==0)
headerStr="First Page header";
else
headerStr="Different header";

HtmlToPdf.Options.HeaderHtmlFormat = headerStr;

}

But the BeforeRenderPage is not firing.

could you please suggest what is the wrong in the code and help to fire the before render event.
Otherwise please suggest alternate option to acheive different header .

Thanks







eo_support
Posted: Tuesday, June 18, 2019 2:15:42 PM
Rank: Administration
Groups: Administration

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

This is a implementation limitation. HtmlToPdf.Options is thread specific so only the HtmlToPdf.Options values set in the thread that actually calls the converter (eventually calls HtmlToPdf.ConvertHtml in the case for MVCToPDF) are used. This is NOT the same thread that calls your action method. Because these are two different thread, we actually serialize the options from one thread and deserlize it in another thread. These will copy all the simple values but not delegates. So delegates do not work in this case.

The recommend method for such scenario is to pass a postHandler when you call RenderAsPDF method. See this topic for more details:

https://www.essentialobjects.com/doc/pdf/web/mvc.aspx#save

Look for item 2 in the "Save Conversion Result into a File" into a file section. While the sample code calls PdfDocument.Save, you can use whatever code you wish. For example, you can use a look to stamp additional header/footer into each page.

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!

EssKay
Posted: Tuesday, June 18, 2019 2:52:23 PM
Rank: Newbie
Groups: Member

Joined: 6/18/2019
Posts: 4
Thank you so much for your quick response with details.


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.