mardi 11 avril 2017

Failed to load pdf document in Mvc Using ITextSharp

Hi this is my Controller code for generate PDf

using System.Web;
using System.Web.Mvc;
using B2B.Common.Models;
using B2B.Common;
using System.Web.Security;
using B2B.BAL;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;

namespace B2B.UI.Areas.Agency.Controllers
{
    [Authorize(Roles = RolesType.Agent)]
    public class MyBookingController : Controller
    {

        #region Hotels Booking
        public ActionResult HotelsBooking()
        {


            StringBuilder pdfBody = new StringBuilder();
            pdfBody.Append("<div class='col-sm-10 dashboard_right_cont pt-2 pb-2' style='margin-bottom:-60px;'>");
            pdfBody.Append("<div id='bookingDetails'  class='registration_cont'>                               ");
            pdfBody.Append("<h4 class='text_blue'><strong>Booking Details</strong> </h4>                       ");
            pdfBody.Append("<hr class='hr_blue'>                                                               ");
            pdfBody.Append("<div class='row'>                                                                  ");
            pdfBody.Append("<div class='col-sm-3'>                                                             ");
            pdfBody.Append("<div class='alert alert-success'>                                                  ");
            pdfBody.Append("<p class='mb-1'>Booking reference number:</p>                                      ");
            pdfBody.Append("<h5><strong>@Html.DisplayFor(model => model.BookingRefNo)</strong></h5>            ");
            pdfBody.Append("<hr class='mt-1 mb-1'>                                                             ");
            pdfBody.Append(" <p class='mb-1'>Booking status:</p>                                               ");
            pdfBody.Append(" <h5><strong>@Html.DisplayFor(model => model.Status)</strong></h5>                 ");
            pdfBody.Append("</div>                                                                             ");
            pdfBody.Append("<div class='alert alert-info'>                                                     ");
            pdfBody.Append("<h6><strong><i class='fa fa-user'></i> Leading Passenger</strong></h6>             ");
            pdfBody.Append("<p class='mb-1'>Passenger name:</p>                                                ");
            pdfBody.Append("<h6><strong>@Html.DisplayFor(model => model.LeadingPassengerName)</strong></h6>    ");
            pdfBody.Append(" <hr class='mt-1 mb-1'>                                                            ");
            pdfBody.Append(" <p class='mb-1'>Nationality:</p>                                                  ");
            pdfBody.Append(" <h6><strong>India</strong></h6>                                                   ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                           ");
            pdfBody.Append("  <p class='mb-1'>Country of residence:</p>                                        ");
            pdfBody.Append("  <h6><strong>India</strong></h6>                                                  ");
            pdfBody.Append("</div>                                                                             ");
            pdfBody.Append("<hr>                                                                               ");
            pdfBody.Append("</div>");



            pdfBody.Append(" </div>                                                                                                                                                                                  ");
            pdfBody.Append(" </div>                                                                                                                                                                                  ");
            pdfBody.Append(" <div class='col-sm-12'>                                                                                                                                                                 ");
            pdfBody.Append(" <div class='hotel_search_details_cont bg_white'>                                                                                                                                        ");
            pdfBody.Append("  <p class='mb-1'>Check-in:</p>                                                                                                                                                          ");
            pdfBody.Append("  <h6><strong>11 Apr 2017 10:30:00</strong></h6>                                                                                                                    ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                                                                                                                                 ");
            pdfBody.Append("  <p class='mb-1'>Check-out:</p>                                                                                                                                                         ");
            pdfBody.Append("  <h6><strong>11 Apr 2017 10:30:00</strong></h6>                                                                                                                      ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                                                                                                                                 ");
            pdfBody.Append("  <p class='mb-1'>Room type:</p>                                                                                                                                                         ");
            pdfBody.Append("  <h6><strong>Sperior</strong></h6>                                                                                                                ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                                                                                                                                 ");
            pdfBody.Append("  <p class='mb-1'>Room occupancy:</p>                                                                                                                                                    ");
            pdfBody.Append("  <h6><strong>2 Adults</strong></h6>                                                                                                                                                     ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                                                                                                                                 ");
            pdfBody.Append("  <p class='mb-1'>Bedding preference:</p>                                                                                                                                                ");
            pdfBody.Append("  <h6><strong>No preference</strong></h6>                                                                                                                                                ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                                                                                                                               ");
            pdfBody.Append("  <p class='mb-1'>Rate basis:</p>                                                                                                                                                        ");
            pdfBody.Append("  <h6><strong>Breakfast</strong></h6>                                                                                                                                                    ");
            pdfBody.Append("  <hr class='mt-1 mb-1'>                                                                                                                                                                 ");
            pdfBody.Append("                                                                                                                                                                                         ");
            pdfBody.Append("  <p class='mb-1'>Additional requests:</p>                                                                                                                                               ");
            pdfBody.Append("  <h6><ol><strong><li>Request for an early check in</li> <li>Request for an late check out</li> <li>Please note that guest is a VIP</li></strong></ol></h6>                              ");

            pdfBody.Append(" </div>   ");
            pdfBody.Append(" </div>   ");
            pdfBody.Append(" </div>   ");
            pdfBody.Append(" </div>   ");
            pdfBody.Append("</div>    ");
            pdfBody.Append("</div>    ");
            pdfBody.Append("</div>    ");
            string pdfContent = Convert.ToString(pdfBody);


            Document document = new Document();         
            document.Open();
            StyleSheet styles = new StyleSheet();
            HTMLWorker hw = new HTMLWorker(document);
            hw.Parse(new StringReader(pdfContent));
            document.Close();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment;filename=PrintVoucher.pdf");
            Response.Flush();
            Response.Clear();




            return View();
        }


    Pdf is downloaded but it shows failed to load pdf document.

I didn't find where I mistake in code.

Please give me correct code Pdf is downloaded but it shows failed to load pdf document.

I didn't find where I mistake in code.

Please give me correct code

Thanks.

Aucun commentaire:

Enregistrer un commentaire