Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用EVOPdf、WebAPI和AngularJS生成PDF_Javascript_Angularjs_Pdf_Asp.net Web Api_Evopdf - Fatal编程技术网

Javascript 使用EVOPdf、WebAPI和AngularJS生成PDF

Javascript 使用EVOPdf、WebAPI和AngularJS生成PDF,javascript,angularjs,pdf,asp.net-web-api,evopdf,Javascript,Angularjs,Pdf,Asp.net Web Api,Evopdf,我使用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 (win

我使用EVOPdfWebAPI控制器向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);
    }
});
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;
APIController方法:

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);
    }
});
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;
当我检查我使用
writealBytes
写出的PDF时,它呈现得非常完美,但当它通过Angular调用返回并在Adobe Reader中打开时,我会收到一条“无效颜色空间”错误消息,该消息会多次弹出,但文档未打开。当我将颜色空间更改为灰度时,PDF会打开,但它是空白的

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

我真的很感激你们能提供的任何帮助,因为我很快就要解决这个问题了,我只需要在电话返回时正确地“转换”文档

提前感谢您的帮助

问候,,
Johann.

问题似乎在于客户端,响应中的字符没有正确解析。我在这里找到了我的解决方案:你试过了吗?这是一篇关于这个话题的好文章。为此,我使用了,它是一个易于集成的解决方案

//安装Puppeter core npm软件包cmd

            npm i puppeteer-core
            # or "yarn add puppeteer-core"
    
           <!-- example.js start-->

       const puppeteer = require('puppeteer');
    
        (async () => {
          const browser = await puppeteer.launch();
          const page = await browser.newPage();
          await page.goto('https://example.com');
          await page.screenshot({ path: 'example.png' });
        
          await browser.close();
        })();
  • 在命令行上执行脚本

    节点hn.js 有关创建pdf的详细信息,请参见Page.pdf()

    示例-在页面上下文中评估脚本

  • 将文件另存为get-dimensions.js

const puppeter=require('puppeter')

  • 在命令行上执行脚本

    节点get-dimensions.js

    请参阅Page.evaluate(),了解有关evaluate和相关方法(如evaluateOnNewDocument和exposeFunction)的更多信息


我刚刚经历了这件事!不幸的是,目前只有在我的手机上,所以不能真正给你一个像样的答案,但给你一个线索,这与ajax无法下载blob/pdf有关。尝试使用filesave.js,我已经成功地将它发送到了除mac上的safari之外的所有版本(到目前为止)。我遇到了完全相同的问题…你找到解决方法了吗?我只是被困在这里。有人有解决办法吗?