0

When I try to generate the pdf; I get the pdf but the data inputted is not shown on the pdf file, while the same info is sent to the database. I seeing that info but I'm not sure what the issue could be as it relates to pdf file. And I have tried it in different browsers and it is still the same thing.

Update

IssueDAO dbdata = new IssueDAO();
        dbdata.connectionString = ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString;
        getIssue.transactionDate = DateTime.Now; //Sets the transaction date to current date
        getIssue.status = -1;
        Item item = new Item();
        try
        {
            dbdata.createIssue(getIssue, item);//Creates the issue in the database
        }
        catch (Exception ex)
        {
            LogWrite logWriter = new LogWrite(ex.ToString());
            ViewBag.errorMessage = "Unable to complete the Issue. Please see Log file for more Information";
            return View("IssueItem", getIssue);

        }


        DataSet ds = dbdata.GetReceipt(getIssue.requisitionNumber);
        LocalReport localreport = new LocalReport();
        localreport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\Reciept.rdlc";
        localreport.DataSources.Add(new ReportDataSource("Receipt_Data", ds.Tables[0]));
        localreport.SetParameters(new ReportParameter("Req_num", getIssue.requisitionNumber));
        string reporttype = "PDF";
        string mimeType;
        string encoding;
        string fileNameExtension = "pdf";
        string deviceInfo = @"<DeviceInfo>              
                 <OutputFormat>PDF</OutputFormat>              
                 <PageWidth>8.5in</PageWidth>              
                 <PageHeight>11in</PageHeight>          
                 <MarginTop>0.25in</MarginTop>          
                 <MarginLeft>0.45in</MarginLeft>            
                 <MarginRight>0.45in</MarginRight>       
                 <MarginBottom>0.25in</MarginBottom></DeviceInfo>";
        Warning[] warnings;
        string[] streams;
        byte[] renderedBytes;
        renderedBytes = localreport.Render(
         reporttype, deviceInfo, out mimeType, out encoding, out fileNameExtension,
         out streams, out warnings);


        var doc = new iTextSharp.text.Document();
        var reader = new PdfReader(renderedBytes);
        using (FileStream fs = new FileStream(Server.MapPath("~/Receipt" +
             Convert.ToString(Session["CurrentUserName"]) + ".pdf"), FileMode.Create))
        {
            PdfStamper stamper = new PdfStamper(reader, fs);
            string Printer = "Xerox Phaser 3635MFP PCL6";
            // This is the script for automatically printing the pdf in acrobat viewer
            stamper.JavaScript = "var pp = getPrintParams();pp.interactive =pp.constants.interactionLevel.automatic; pp.printerName = " +
                           Printer + ";print(pp);\r";
            stamper.Close();
        }
        reader.Close();
        FileStream fss = new FileStream(Server.MapPath("~/Receipt.pdf"), FileMode.Open);
        byte[] bytes = new byte[fss.Length];
        fss.Read(bytes, 0, Convert.ToInt32(fss.Length));
        fss.Close();
        System.IO.File.Delete(Server.MapPath("~/Receipt.pdf"));

        //Here we returns the file result for view(PDF)
        ModelState.Clear();
        Session.Clear(); //Clears the session variable for reuse 
        return File(bytes, "application/pdf");
    }

2 Answers2

0

I did a little research on this topic and I curious why didn't you return the FileStream for the pdf you created inside of your using statement. Rather then trying to what looks like open it again and then send the bytes?

I found this article and maybe this will help because it eliminates the need for the reader and everything. Look at the first answer.

    DataSet ds = dbdata.GetReceipt(getIssue.requisitionNumber);
    ReportDataSource reportDataSource = new ReportDataSource();
    reportDataSource.Value = ds.Tables[0];
    reportDataSource.Name = "Receipt_Data";
    LocalReport localreport = new LocalReport();
    localreport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\Reciept.rdlc";
    localreport.DataSources.Add(reportDataSource);
    localreport.SetParameters(new ReportParameter("Req_num", getIssue.requisitionNumber));
    string reporttype = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension = "pdf";
    string deviceInfo = @"<DeviceInfo>              
             <OutputFormat>PDF</OutputFormat>              
             <PageWidth>8.5in</PageWidth>              
             <PageHeight>11in</PageHeight>          
             <MarginTop>0.25in</MarginTop>          
             <MarginLeft>0.45in</MarginLeft>            
             <MarginRight>0.45in</MarginRight>       
             <MarginBottom>0.25in</MarginBottom></DeviceInfo>";
    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;
    renderedBytes = localreport.Render(
     reporttype, deviceInfo, out mimeType, out encoding, out fileNameExtension,
     out streams, out warnings);


    var doc = new iTextSharp.text.Document();
    var reader = new PdfReader(renderedBytes);
    using (FileStream fs = new FileStream(Server.MapPath("~/Receipt" +
         Convert.ToString(Session["CurrentUserName"]) + ".pdf"), FileMode.Create))
    {
        PdfStamper stamper = new PdfStamper(reader, fs);
        string Printer = "Xerox Phaser 3635MFP PCL6";
        // This is the script for automatically printing the pdf in acrobat viewer
        stamper.JavaScript = "var pp = getPrintParams();pp.interactive =pp.constants.interactionLevel.automatic; pp.printerName = " +
                       Printer + ";print(pp);\r";
        stamper.Close();
    }
    reader.Close();
    FileStream fss = new FileStream(Server.MapPath("~/Receipt.pdf"), FileMode.Open);
    byte[] bytes = new byte[fss.Length];
    fss.Read(bytes, 0, Convert.ToInt32(fss.Length));
    fss.Close();
    System.IO.File.Delete(Server.MapPath("~/Receipt.pdf"));

    //Here we returns the file result for view(PDF)
    ModelState.Clear();
    Session.Clear(); //Clears the session variable for reuse 
    return File(bytes, "application/pdf");
}

Creating a PDF from a RDLC Report in the Background

Other good examples

Kyle Pearson
  • 107
  • 8
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/157524/discussion-on-answer-by-kyle-pearson-printing-pdf-from-asp-net-mvc-project). – Andy Oct 26 '17 at 01:00
  • Anything for me as yet Kyle –  Oct 26 '17 at 22:34
  • @LeonCharles did you use this article? https://www.codeproject.com/Tips/569335/Automatically-Printing-an-RDLC-file-in-ASP-NET-MVC – Kyle Pearson Oct 27 '17 at 12:33
  • no i didn't but thanks for sharing but i don't see what i did differently –  Oct 27 '17 at 15:20
  • Hey Kyle I really appreciate your help even thou this wasn't the issue at the end of the day. –  Oct 31 '17 at 16:31
  • Your welcome and once you find the exact cause post it in an answer to complete this! – Kyle Pearson Oct 31 '17 at 16:35
0

!st get you data to client side somehow... Ajax , viewbag , session ..whatever

jsPDF is able to use plugins. In order to enable it to print HTML, you have to include certain plugins and therefore have to do the following:

Go to https://github.com/MrRio/jsPDF and download the latest Version.
Include the following Scripts in your project:
jspdf.js
jspdf.plugin.from_html.js
jspdf.plugin.split_text_to_size.js
jspdf.plugin.standard_fonts_metrics.js

If you want to ignore certain elements, you have to mark them with an ID, which you can then ignore in a special element handler of jsPDF. Therefore your HTML should look like this:

*

<!DOCTYPE html>
<html>
  <body>
    <p id="ignorePDF">don't print this to pdf</p>
    <div>
      <p><font size="3" color="red">print this to pdf</font></p>
    </div>
  </body>
</html>

*

Then you use the following JavaScript code to open the created PDF in a PopUp:

var doc = new jsPDF();          
var elementHandler = {
  '#ignorePDF': function (element, renderer) {
    return true;
  }
};
var source = window.document.getElementsByTagName("body")[0];
doc.fromHTML(
    source,
    15,
    15,
    {
      'width': 180,'elementHandlers': elementHandler
    });

doc.output("dataurlnewwindow");

For me this created a nice and tidy PDF that only included the line 'print this to pdf'.

Rohit Kumar
  • 1,777
  • 2
  • 13
  • 26