使用EVOPdf,WebAPI和AngularJS生成PDF

Joh*_*arx 13 javascript pdf evopdf asp.net-web-api angularjs

我在使用EVOPdf将PDF 从WebAPI控制器渲染到AngularJS应用时遇到问题。

到目前为止,这是我的代码:

角度通话:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

当我检查使用编写的PDF时WriteAllBytes,它可以完美呈现,但是当它通过Angular调用返回并在Adobe Reader中打开时,我收到“无效色彩空间”错误消息,该错误消息弹出了很多次,但是文档没有打开。当我将色彩空间更改为GrayScale时,PDF将打开,但它是空白的。

我感觉是ByteArrayContent导致问题的是转换,因为这是在实际创建PDF和将其发送回Angular调用之间发生的唯一事情,但是我遇到了障碍,无法确定找出问题所在。

非常感谢你们能提供的任何帮助,因为我已经很接近解决这个问题了,我只需要文档可以在通话返回时正确地进行“转换”。

在此先感谢您的帮助。

尊敬的约翰。

Kri*_*óth 0

问题似乎出现在客户端,响应中的字符未正确解析。对于任何为此苦苦挣扎的人,我在这里找到了我的解决方案:所以问题