服务器生成的PDF angularjs具有较长的帖子内容

时间:2022-09-03 21:17:12

After trying a lot of different approaches, nothing worked for me.

在尝试了很多不同的方法之后,没有什么对我有用。

The most similar question is this one How to read pdf stream in angularjs

最相似的问题是这一个如何在angularjs中阅读pdf流

I have an AngularJS app that sends a request to my PHP-File to generate a PDF file. My controller has the following function that I call from a button via ng-click:

我有一个AngularJS应用程序,它向我的PHP文件发送请求以生成PDF文件。我的控制器具有以下功能,我通过ng-click从按钮调用:

$scope.print = function() {
    $http({
        method : "post",
        url : '/pdf/print_processor.php',
        data : printdata,
        //responseType : 'arraybuffer',
        headers : {
            'Content-type' : 'application/json'
        }
    }).success(function(response, status, headers) {                        
     var file = new Blob([response], {type: 'application/pdf'});
        var fileURL = URL.createObjectURL(file);
        window.open(fileURL);
    });
}   

The main reason was that I have to ship some more data to the PHP file. The printdata variable contains a nested JSON object with more than 12 KB. So I could not use a normal GET or pass this data via a normal link as part of the URL.

主要原因是我必须将更多数据发送到PHP文件。 printdata变量包含一个超过12 KB的嵌套JSON对象。所以我无法使用普通的GET或通过普通链接传递此数据作为URL的一部分。

The request is working, because I can call the print_processor.php directly (which gives me the PDF) and I use the same approach for creating sending email. Like in the mentioned question above, my response also contains the pdf itself:

请求正在运行,因为我可以直接调用print_processor.php(这给了我PDF),并且我使用相同的方法来创建发送电子邮件。就像上面提到的问题一样,我的回复也包含了pdf本身:

%PDF-1.5
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode /Length 85>>
stream
...

But now I just want to give the PDF file to the browser exaclty like i would open the PHP-file directly... Although the controller opens a new window with the URL "blob:7e2d358f-00a1-4e33-9e7e-96cfe7c02688", the page is empty and I think, the URL is too short for the whole blob...

但现在我只想将PDF文件提供给浏览器,就像我直接打开PHP文件一样...虽然控制器打开一个新窗口,其URL为“blob:7e2d358f-00a1-4e33-9e7e-96cfe7c02688”,页面是空的,我认为,URL太短了整个blob ...

Some other similar questions are:

其他一些类似的问题是:

How to display a server side generated PDF stream in javascript sent via HttpMessageResponse Content

如何在通过HttpMessageResponse Content发送的javascript中显示服务器端生成的PDF流

AngularJS: download pdf file from the server

AngularJS:从服务器下载pdf文件

How to read pdf stream in angularjs

如何在angularjs中阅读pdf流

AngularJS $http-post - convert binary to excel file and download

AngularJS $ http-post - 将二进制文件转换为excel文件并下载

When I use the often mentioned responseType "arraybuffer" (which I commented out in the code above) - the result of the PHP is "null" and not the content of the PHP file. When it is not used, I get the PDF file... May be that is a hint?!

当我使用经常提到的responseType“arraybuffer”(我在上面的代码中注释掉)时 - PHP的结果是“null”而不是PHP文件的内容。当它没有被使用时,我得到PDF文件......可能是一个提示?!

My Angular version is 1.2.26.

我的Angular版本是1.2.26。

A total other solution I tried was calling the PHP directly (without ajax/angular controller) by using a form with post-method and a hidden field for the printdata. Since the printdata is a multi-level nested json object, it is not really possible to put it into the hidden field...

我尝试的另一个解决方案是直接调用PHP(没有ajax / angular controller),使用带有post方法的表单和printdata的隐藏字段。由于printdata是一个多层嵌套的json对象,因此无法将其放入隐藏字段中...

I hope someone has any other idea or find a mistake!?

我希望有人有任何其他想法或发现错误!?

1 个解决方案

#1


1  

Add params responseType, header and cache in $http like this :

在$ http中添加params responseType,header和cache,如下所示:

$scope.print = function() {
    $http({
        method : "post",
        url : '/pdf/print_processor.php',
        data : printdata,
        responseType : 'arraybuffer',
        headers: {
                            accept: 'application/pdf'
        },
        cache: true,
    }).success(function(response, status, headers) {                        
     var file = new Blob([response], {type: 'application/pdf'});
        var fileURL = URL.createObjectURL(file);
        window.open(fileURL);
    });
}   

#1


1  

Add params responseType, header and cache in $http like this :

在$ http中添加params responseType,header和cache,如下所示:

$scope.print = function() {
    $http({
        method : "post",
        url : '/pdf/print_processor.php',
        data : printdata,
        responseType : 'arraybuffer',
        headers: {
                            accept: 'application/pdf'
        },
        cache: true,
    }).success(function(response, status, headers) {                        
     var file = new Blob([response], {type: 'application/pdf'});
        var fileURL = URL.createObjectURL(file);
        window.open(fileURL);
    });
}