Welcome Guest Search | Active Topics | Sign In | Register

Problem with RenderAsPDF attribute and (I think) async controller Options
Jonathan Hallee
Posted: Friday, July 31, 2015 11:18:41 AM
Rank: Newbie
Groups: Member

Joined: 7/31/2015
Posts: 6
So I have been trying to figure out this problem for a long time now, but now I require help :p

I think that there is a problem using the RenderAsPDF attribute on an async MVC controller such as :

[RenderAsPDF]
[AllowAnonymous]
public async Task<ActionResult> GenerateSummary(long distributorId, long quotationId)

The problem is the following: It does not take into account my HtmlToPDF options defined as such:

HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f);
HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f);

// And then I return my view with a view model.

return this.View("~/Views/QuotationDocuments/Summary/QuotationSummary.cshtml", viewModel);

However, the PDF is always rendered with 1 inch margins: http://grab.by/JgyC

Has this ever happened to someone?

Thanks,
Jonathan
eo_support
Posted: Friday, July 31, 2015 11:40:32 AM
Rank: Administration
Groups: Administration

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

Thanks for posting in the forum. This appears to be a problem on our side. Can you create a small test project and send the test project to us? See here for more instructions on how to send the test project:

http://www.essentialobjects.com/forum/test_project.aspx

We will look into it as soon as we receive it.

Thanks!

Jonathan Hallee
Posted: Friday, July 31, 2015 3:45:47 PM
Rank: Newbie
Groups: Member

Joined: 7/31/2015
Posts: 6
Just sent the test project with additional informations on what to look for!
eo_support
Posted: Friday, July 31, 2015 4:07:14 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
Thank you very much for the test project. We will look into it and get back to you as soon as possible.
Jonathan Hallee
Posted: Monday, August 3, 2015 3:39:13 PM
Rank: Newbie
Groups: Member

Joined: 7/31/2015
Posts: 6
Hi! Have you guys has the time to look at the issue?

Thanks!
eo_support
Posted: Monday, August 3, 2015 8:23:26 PM
Rank: Administration
Groups: Administration

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

Please put the corresponding code inside AsycManager.Sync call:

Code: C#
AsyncManager.Sync(() =>
{
    HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f);
    HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f);
});


This will run the code inside the original HTTP context thread. Because HtmlToPdf.Options maintains a different value per thread, as long as you switch to the correct thread to set it, it will work for you.

Thanks!
Jonathan Hallee
Posted: Monday, August 3, 2015 9:04:25 PM
Rank: Newbie
Groups: Member

Joined: 7/31/2015
Posts: 6
Hi,

Thanks for your reply. I have tried the solution you mentionned but unfortunately it does not seem to be working. I have tried it in the test project I have sent you and I still get the 1 inch margins sometimes.

Here is the modified code for the async method now:

Code: C#
[RenderAsPDF]
        [AllowAnonymous]
        public async Task<ActionResult> AsyncPdf()
        {
            // This method runs asynchronously.
            await Task.Run(() => Allocate());

            this.AsyncManager.Sync(() =>
            {
                HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f);
                HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f);
            });

            return this.View("AsyncPDF");
        }


Am i not applying your fix correctly? (Note that the code snippet due to the forum displays only Task has return type but in fact is Task<ActionResult>)

Thanks.
Jonathan Hallee
Posted: Tuesday, August 4, 2015 5:38:05 PM
Rank: Newbie
Groups: Member

Joined: 7/31/2015
Posts: 6
Hi,

Any news for me?

Thanks!
eo_support
Posted: Wednesday, August 5, 2015 3:02:29 PM
Rank: Administration
Groups: Administration

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

We tested the code here and it works fine. Another thing you can try is to set the PageSize and OutputArea inside BeforeConvertHandler. BeforeConvertHandler is a callback method that you can pass to the RenderAsPDF method:

http://www.essentialobjects.com/doc/eo.pdf.mvc.mvctopdf.renderaspdf_overload_1.aspx

You can pass a callback method to the first argument and then set the HtmlToPdf.Options inside that callback method. This method is guaranteed to be called in the same thread as the converter.

Note that this method is only added not long ago. So if you have an older version, you may not have this method. In that case please try to update to the latest build first.

Thanks!
Jonathan Hallee
Posted: Thursday, August 6, 2015 3:44:48 PM
Rank: Newbie
Groups: Member

Joined: 7/31/2015
Posts: 6
Ha...

Apparently the fix is not working on my dev machine, but it is working on our staging environment on Azure. I'm guessing .NET 4.6 or IIS Express caused the problem for some reason...

Anyways it's working now so thanks for the help!
eo_support
Posted: Thursday, August 6, 2015 4:18:33 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
Great. Thank you very much for the update. Please feel free to let us know if there is anything else.


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.