我需要检索然后显示pdf文件。我有工作代码从数据库中检索图像,转换为.pdf,并将其作为JSON返回。我可以通过将它变成blob来显示这个就好了,但是因为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
属性?
答案 0 :(得分:0)
&#34; GhostScript的&#34;可以帮到你。请查看链接How to use Ghostscript for converting PDF to Image和https://ghostscriptnet.codeplex.com/
答案 1 :(得分:0)
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;
}