【问题标题】:How to create a pdf using webservice如何使用网络服务创建 pdf
【发布时间】:2012-03-28 06:40:31
【问题描述】:

我正在使用 jquery ajax 从 web 服务调用函数。

在该函数中,我正在使用 itextsharp 工具创建一个 pdf 文件。 我希望我创建的 pdf 文件在返回时应该在浏览器中打开。

谁能帮助我的返回类型应该是什么

下面是我在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);
}

谁能告诉我我做错了什么

【问题讨论】:

    标签: asp.net web-services c#-4.0 jquery itextsharp


    【解决方案1】:

    简短的回答是,“不要为此使用 AJAX”,这会造成不必要的复杂性。相反,只需通过浏览器发出正常的 GET/POST 请求。如果你愿意,你仍然可以使用 JavaScript,但重要的是你让浏览器发出请求,以便它可以接收响应。

    长答案是……

    Web 服务器响应来自 Web 浏览器的请求,并且事情会按照您的预期(通常)发生。 Web 浏览器有一个他们知道的内容类型列表,并使用此列表有时解析服务器的响应,有时将其交给第 3 方应用程序。一旦你开始使用 AJAX 和其他类似技术,你就打破了这个模型,并说你想要处理处理而不是浏览器。浏览器将代理您的请求和服务器的响应,但除非您告诉它,否则它不会做任何事情。这对于类似字符串的东西非常有用,但在处理二进制数据时会变得更加复杂。

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      相关资源
      最近更新 更多