【发布时间】:2015-11-03 05:42:03
【问题描述】:
我在使用 EVOPdf 从 WebAPI 控制器 向 AngularJS 应用程序呈现 PDF 时遇到问题。
这是我目前的代码:
角度调用:
var url = 'api/form/build/' + id;
$http.get(url, null, { responseType: 'arraybuffer' })
.success(function (data) {
var file = new Blob([data], { type: 'application/pdf' });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file);
}
else {
var objectUrl = URL.createObjectURL(file);
window.open(objectUrl);
}
});
APIController 方法:
var url = "http://localhost/index.html#/form/build/" + id;
#region PDF Document Setup
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
//htmlToPdfConverter.HtmlViewerWidth = 1024; //default
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait;
htmlToPdfConverter.ConversionDelay = 3;
htmlToPdfConverter.MediaType = "print";
htmlToPdfConverter.PdfDocumentOptions.LeftMargin = 10;
htmlToPdfConverter.PdfDocumentOptions.RightMargin = 10;
htmlToPdfConverter.PdfDocumentOptions.TopMargin = 10;
htmlToPdfConverter.PdfDocumentOptions.BottomMargin = 10;
htmlToPdfConverter.PdfDocumentOptions.TopSpacing = 10;
htmlToPdfConverter.PdfDocumentOptions.BottomSpacing = 10;
htmlToPdfConverter.PdfDocumentOptions.ColorSpace = ColorSpace.RGB;
// Set HTML content destination in PDF page
htmlToPdfConverter.PdfDocumentOptions.Width = 640;
htmlToPdfConverter.PdfDocumentOptions.FitWidth = true;
htmlToPdfConverter.PdfDocumentOptions.StretchToFit = true;
#endregion
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
string outPdfFile = @"c:\temp\forms\" + id + ".pdf";
System.IO.File.WriteAllBytes(outPdfFile, outPdfBuffer);
HttpResponseMessage result = null;
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(outPdfBuffer.ToArray());
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "filename.pdf";
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
当我检查我使用 WriteAllBytes 写出的 PDF 时,它呈现完美,但是当它通过 Angular 调用返回并在 Adobe Reader 中打开时,我得到一个 “无效色彩空间”弹出很多次的错误消息,但未打开文档。当我将色彩空间更改为灰度时,PDF 会打开但它是空白的。
我感觉是 ByteArrayContent 转换导致了问题,因为这是在实际创建 PDF 并将其发送回 Angular 调用之间唯一发生的事情,但我遇到了难题不知道是什么问题。
非常感谢你们能提供的任何帮助,因为我已经快要解决这个问题了,我只需要在电话返回时正确“转换”文档。
提前感谢您的帮助。
问候, 约翰。
【问题讨论】:
-
我刚刚经历过这件事!不幸的是,目前只能在我的手机上,所以不能真正给你一个像样的答案,但给你一个线索,这与 ajax 无法下载 blob/pdf 有关。尝试使用 filesave.js,我已经设法让它适用于 Mac 上除 safari 之外的所有用户(到目前为止)。
-
我遇到了完全相同的问题...您找到解决方法了吗?我只是卡住了:(
-
这里也一样。有人有解决办法吗?
标签: javascript angularjs pdf asp.net-web-api evopdf