简体   繁体   中英

How to create a pdf using webservice

I am using jquery ajax to call function from webservice.

In that function I am creating a pdf file using itextsharp tool. I want that my pdf file created should open in browser when return.

can anyone help me what should be my return type for that

Below is the code I am using in webservice

 public void GeneratePDf(string ID) {
            string attachment = "attachment; filename=" + ID + ".pdf";
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "application/pdf";
            StringWriter stw = new StringWriter();
            HtmlTextWriter htextw = new HtmlTextWriter(stw);
            htextw.AddStyleAttribute("font-size", "12px");
            htextw.AddStyleAttribute("color", "Black");
            Page pg = new Page();
            HtmlForm frm = new HtmlForm();
            pg.EnableEventValidation = false;

            pg.RenderControl(htextw);
            Document document = new Document();

            document = new Document(PageSize.A4, 10, 10, 0, 0);
            PdfWriter.GetInstance(document, HttpContext.Current.Response.OutputStream);
            document.Open();
            Font verdana = FontFactory.GetFont("Verdana", 10, Font.BOLD, new CMYKColor(75, 68, 67, 90));
            PdfPCell blank1 = new PdfPCell(new Phrase("Hello ", verdana));
            document.Add(blank1);
            //document.Add(tablegrid);
            StringReader str = new StringReader(stw.ToString());
            HTMLWorker htmlworker = new HTMLWorker(document);
            htmlworker.Parse(str);

            document.Close();
            HttpContext.Current.Response.Write(document);
}

Can anyone tell me what I am doing wrong

The short answer is, "don't use AJAX for this", you are creating unnecessary complication. Instead, just make a normal GET/POST request via your browser. You can still use JavaScript if you want but the important part is that you have the browser make the request so that it can receive the response.

The long answer is...

Web servers respond to requests from web browsers and things happen just as you expect them to (usually). Web browsers have a list of content types that they are aware and use this list to sometimes parse the server's response and sometimes hand it off to a 3rd party application. Once you start messing around with AJAX and other similar technologies you break this model and are saying that you want to handle the processing instead of the browser. The browser will broker your request and the server's response but otherwise it won't do anything unless you tell it to. This works great for string-like things but gets much more complicated when you deal with binary data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM