【问题标题】:Dynamically create .pdf file on server在服务器上动态创建 .pdf 文件
【发布时间】:2018-04-26 11:36:11
【问题描述】:

我需要检索然后显示一个 pdf 文件。我有从数据库中检索图像、转换为 .pdf 并将其作为 JSON 返回的工作代码。我可以通过将其制作成 blob 在 chrome 中很好地显示它,但是由于 IE 拒绝支持数据 URI,我想我可以在服务器上生成一个临时 pdf 文件,然后像这样链接到它,正如网站上其他地方所建议的那样:

<iframe style="width: 100%; height: 100%;" frameborder="0" scrolling="no" id="myFrame">
    <p>It appears your web browser doesn't support iframes.</p>
</iframe>

然后在.js文件中设置src属性:

 $('#myFrame').attr('src', 'http://www.example.com/tempPDFname.pdf');

如何生成此文件并使其在服务器 (C#) 上可用,以便我可以设置 src 属性?

【问题讨论】:

  • 这完全取决于你的服务器是如何构建的。您只需要创建一个为 PDF 文件提供服务的操作/处理程序。

标签: javascript c# pdf


【解决方案1】:

“GhostScript”可能会对您有所帮助。请检查链接How to use Ghostscript for converting PDF to Imagehttps://ghostscriptnet.codeplex.com/

【讨论】:

    【解决方案2】:

    How to return a PDF from a Web API application

    [HttpGet]
    [Route("documents/{docid}")]
    public HttpResponseMessage Display(string docid) {
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest);
    var documents = reader.GetDocument(docid);
    if (documents != null && documents.Length == 1) {
        var document = documents[0];
        docid = document.docid;
        byte[] buffer = new byte[0];
        //generate pdf document
        MemoryStream memoryStream = new MemoryStream();
        MyPDFGenerator.New().PrintToStream(document, memoryStream);
        //get buffer
        buffer = memoryStream.ToArray();
        //content length for use in header
        var contentLength = buffer.Length;
        //200
        //successful
        var statuscode = HttpStatusCode.OK;
        response = Request.CreateResponse(statuscode);
        response.Content = new StreamContent(new MemoryStream(buffer));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        response.Content.Headers.ContentLength = contentLength;
        ContentDispositionHeaderValue contentDisposition = null;
        if (ContentDispositionHeaderValue.TryParse("inline; filename=" + document.Name + ".pdf", out contentDisposition)) {
            response.Content.Headers.ContentDisposition = contentDisposition;
        }
    } else {
        var statuscode = HttpStatusCode.NotFound;
        var message = String.Format("Unable to find resource. Resource \"{0}\" may not exist.", docid);
        var responseData = responseDataFactory.CreateWithOnlyMetadata(statuscode, message);
        response = Request.CreateResponse((HttpStatusCode)responseData.meta.code, responseData);
    }
    return response;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-08
      • 2015-05-01
      • 2018-10-22
      • 2012-04-20
      • 2023-04-07
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多