Rotativa ViewAsPDF CSS Sizing / Dimensions
Asked Answered
C

4

12

I am rendering an HTML View in ASP.Net MVC to PDF using Rotativa ViewAsPdf method. I am setting the ouput to be of A4 Size, Portrait and no margins, by setting:

new ViewAsPdf(MVCCfpFormatter.Members.Views.FlightPlansFullPagePrint, model)
                    {
                       // FileName = flightPlan.ListingItemDetailsModel.FlightDetails + ".pdf",
                        PageSize = Size.A4,
                        PageOrientation = Orientation.Portrait,
                        PageMargins = new Margins(0, 0, 0, 0),
                        PageWidth = 210,
                        PageHeight = 297
                    };

Then within the CSS, I am setting an element to have 210mm width, which should spread the entire width of the page, yet in the output PDF, the 210mm width is not representing the entire width of the PDF but less. By trial and error, the total width of the generated PDF seems to be around 246mm.

Any ideas why this could be happening?

Contraposition answered 8/7, 2014 at 12:34 Comment(0)
A
13

If you are still having issues ive done more research on rotativa. You can use custom switches from wkhtmltopdf. Try and add

CustomSwitches = "--disable-smart-shrinking". 

That stops the program from auto resizing your things. From there you can adjust your html page correctly to get the size you want on the pdf.

Asir answered 7/8, 2014 at 21:39 Comment(6)
You already have an answer to this question! You should edit your exiting answer instead of posting a new one.Gladiolus
I'll try it out @Asir and let you know the results, thanks for the comment.Contraposition
Tried this out and it still seems to leave a discrepancy of half an inch when trying to position items correctly through CSSContraposition
Have you made sure you have no other css or scripts modifying your page before it renders? I can seem to get it to work fine for my application.Asir
thank you for your response, it's very helpful. I have problem about Unicode, when generate PDF some Unicode is upset. do you have any Idea ?Analyze
check out these switches: wkhtmltopdf.org/usage/wkhtmltopdf.txtAdrea
A
5

I think I can help you out.

 {
               FileName = Name + ".pdf",
               PageOrientation = Rotativa.Options.Orientation.Landscape,
               PageSize = Rotativa.Options.Size.A4

 }

Is what works for me with 1.6.3. Id imagine you would need to follow that syntax to set the other things you want.

Asir answered 11/7, 2014 at 7:0 Comment(6)
What do you mean? Isn't that the same syntax like I have showed above, just using the full namespace, Rotativa.Options... ?Contraposition
Yes it is, what I meant was I could not get mine to work correctly without using the full namespace. If that does not help fix your problem it sounds like it may be in your CSS?Asir
My full namespaces where including using the using keywords at the top which I didn't include here. When you managed to try yours, did it work out perfectly with the correct given dimensions?Contraposition
Yeah it works very well. Maybe just try set the pagesize like I did and see what you get? I have not tried to print out a pdf yet, but in reader it formats perfectly.Asir
You did the same thing as I did @Asir so it will output the same results. Can you please try it out with exact correct dimensions like I asked in my question before?Contraposition
After experimenting with my own pdf render problem I think I may know. Have you tried setting the Pagesize with no dimeions? or just the width and height? When you specify the page size it has default dimensions it uses. By setting a width and height as well it may be stuffing with it. I am also having trouble trying to fit it all onto one page.Asir
F
0

if you are using mvc 4 ,should use this format

         public ActionResult yourAction()
      {

       var yourModel;


        var yourpdf= new PartialViewAsPdf(yourModel)
        {
            RotativaOptions = new DriverOptions()
            {
                PageOrientation=Orientation.Landscape,
                PageSize = Size.A4,
                IsLowQuality=true
                and other your Customiz

            }
        };
        return yourpdf;

    }
Future answered 6/2, 2019 at 18:25 Comment(0)
S
0

for MVC before any thing don't forget to add

using Rotativa.MVC;
using Rotativa;

and for using customizing option do like this

  return new ActionAsPdf
                ("ActionName")
                {
                    FileName = "Filename.pdf",
                    RotativaOptions = new Rotativa.Core.DriverOptions()
                    {
                        PageSize=Rotativa.Core.Options.Size.A4,
                    }
                   
                };
Sleave answered 21/11, 2020 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.