gpt4 book ai didi

javascript - 使用 EVOPdf、WebAPI 和 AngularJS 生成 PDF

转载 作者:可可西里 更新时间:2023-11-01 02:12:49 25 4
gpt4 key购买 nike

我在使用 EVOPdfWebAPI Controller AngularJS 应用程序呈现 PDF 时遇到问题。

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

Angular 调用:

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 调用返回并在 Adob​​e Reader 中打开时,我得到一个 “无效的色彩空间" 多次弹出错误消息,但文档未打开。当我将色彩空间更改为 GrayScale 时,PDF 打开但它是空白的。

我感觉是 ByteArrayContent 转换导致了这个问题,被认为是实际创建 PDF 并将其发送回 Angular 调用之间唯一发生的事情,但我碰壁了,想不通是什么问题。

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

在此先感谢您的帮助。

问候,约翰。

最佳答案

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

关于javascript - 使用 EVOPdf、WebAPI 和 AngularJS 生成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com