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

Custom Page Size not working when pdf is saved to App_Data #35

Closed jldelacruz closed 9 years ago

jldelacruz commented 9 years ago

Custom page size on preview is working 612f x 396f but the saved file to App_Data is whole Letter size.

   public ActionResult SaveToAppData()
    {
        var model = new recordListModel();
        model.records = db.Records.Select(x => new recordModel
        {
            Id = x.Id,
            Date = x.Date,
            PaidTo = x.PaidTo,
            CheckNo = x.CheckNo,
            Payor = x.Payor
        }).ToList();

        byte[] pdfOutput = ControllerContext.GeneratePdf(model, "Print");
        string fullPath = Server.MapPath("~/App_Data/FreshlyMade.pdf");

        if (System.IO.File.Exists(fullPath))
        {
            System.IO.File.Delete(fullPath);
        }
        System.IO.File.WriteAllBytes(fullPath, pdfOutput);

        return View("SaveToAppData");
    }

    public ActionResult Print()
    {
        var model = new recordListModel();
        model.records = db.Records.Select(x => new recordModel
        {
            Id = x.Id,
            Date = x.Date,
            PaidTo = x.PaidTo,
            CheckNo = x.CheckNo,
            Payor = x.Payor
        }).ToList();

        return new PdfActionResult(model, (writer, document) =>
        {
            document.SetPageSize(new Rectangle(612f, 396f, 90));
            document.NewPage();
        });
    }