Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Three steps I am following.

Create PDF in one view.
Store in database in byte[] form which column is varbinary(max)
Retrieve from database and show as PDF when user click.
Creating PDF:
C#
public byte[] PDF(List<DeliveryOrderDetail> deliveryOrdList)
        {
            byte[] result;
            string headingText = "Guide";
            try
            {

                using (MemoryStream ms = new MemoryStream())
                {
                    Document doc = new Document();
                    PdfWriter.GetInstance(doc, ms).CloseStream = false;
                    PdfPTable pdfTab = new PdfPTable(3);
                    Paragraph heading = new Paragraph(headingText, new Font(Font.TIMES_ROMAN, 18f, Font.NORMAL, Color.BLACK));
                    Paragraph ItemDetail = new Paragraph("Items in Delivery Order Box:", new Font(Font.TIMES_ROMAN, 12f, Font.NORMAL, Color.BLACK));
                    heading.Alignment = Element.ALIGN_CENTER;
                    ItemDetail.Alignment = Element.ALIGN_CENTER;
                    doc.Open();
                    doc.Add(heading);
                    pdfTab.HorizontalAlignment = 1; // 0- Left, 1- Center, 2- right
                    pdfTab.SpacingBefore = 20f;
                    pdfTab.SpacingAfter = 20f;
                    // pdfTab.AddCell("Sl. No.");
                    pdfTab.AddCell("ItemName");
                    pdfTab.AddCell("BoxId");
                    pdfTab.AddCell("Manufacturer");
                    doc.Add(ItemDetail);
                    foreach (var item in deliveryOrdList)
                    {

                        pdfTab.AddCell(item.Name);
                        pdfTab.AddCell(item.Id.ToString());
                        pdfTab.AddCell(item.Manufacturer);

                    }

                    doc.Add(pdfTab);
                    doc.Close();
                    result = ms.GetBuffer();
                }


                return result;

            }
            catch(Exception e )
            {

            }
            return null;
        }


Storing result which is byte[] into database table with db.savechanges();

On image click :

var pdf will get the database field which contains binary data

jQuery: when image is clicked go to controller
JavaScript
<script type="text/javascript">
     $(document).ready(function () {
         $('.Pdf').click(function () {

             var pdf = $(this).closest('tr').find('td:eq(0) input').val();;

             alert(pdf);
             $.ajax({
                 url: "/ship/PDF",
                 type: "POST",
                 dataType: "json",
                 data:JSON.stringify({ pdf: pdf }),
                 //data:{pdf:pdf},
                 cache: false,
                 dataType: "json",
                 contentType: "application/json; charset=utf-8"

             })
         })
     })
 </script>

In Controller: Following method is written which accept as string :
C#
[HttpPost]
public ActionResult PDF(string  pdf)
{
    try
    {

        byte[] byteInfo =  System.Text.Encoding.UTF8.GetBytes(pdf);
        MemoryStream ms = new MemoryStream();
        workStream.Write(byteInfo, 0, byteInfo.Length);
        workStream.Position = 0;


        return new FileStreamResult(ms, "application/pdf");

    }
    //to catch the exception
    catch (Exception _Exception)
    {

    }
    return null;
}

Here I am unable to see pdf.in browser when image is clicked. I have tried all options but unable to get it. If I write it in file. PDF will not get opened
C#
System.IO.File.WriteAllBytes(@"D:\personal\testpdf.pdf", byteInfo);
Posted
Updated 13-Jun-14 0:03am
v2
Comments
Prasad Khandekar 13-Jun-14 6:13am    
It looks like encoding issue. If I am not wrong you are sending the PDF data to browser and upon image click you are sending this data back to server which then streams it back to the page.

Please note that you will not be able to download the PDF via AJAX.

If you do not want to want change the current way, I suggest you send the ODF bytes in Base64 encoded form (System.Convert.ToBase64String(byte[]) & while sending out use System.Convert.FromBase64String(base64EncodedData); and stream the resulting byte array with Content-Disposition header. Also on Image Click instead of giving an Ajax Call open a new window and then make a POST request in that window.

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900