Created by angularjs pdf server with long message

Having tried many different approaches, nothing worked for me.

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

I have an AngularJS application that sends a request to my PHP file to create a PDF file. My controller has the following function that I call using the ng-click button:

$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 had to send some more data to a PHP file. The printdata variable contains a nested JSON object with more than 12 KB. Therefore, I could not use a regular GET or pass this data via a regular link as part of the URL.

The request works because I can directly call print_processor.php (which gives me a PDF), and I use the same approach to create an email send. As in the above question above, my answer also contains the PDF itself:

 %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 provide the PDF file to the exaccty browser, since I would immediately open the PHP file ... Although the controller opens a new window with the URL "blob: 7e2d358f-00a1-4e33-9e7e- 96cfe7c02688", the page is empty, and I I think the URL is too short for the whole blob ...

Some other similar questions:

How to display server-side generated PDF stream in javascript sent via HttpMessageResponse content

AngularJS: download pdf file from server

How to read pdf stream in angularjs

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

When I use the often referred to responseType "arraybuffer" (which I commented on in the above code), the result of PHP is "null", not the contents of the PHP file. When not in use, I get a PDF file ... Maybe this is a hint ?!

My version of Angular is 1.2.26.

A common other solution that I tried called PHP directly (without an ajax / angular controller) using a form with a post method and a hidden field for printdata. Since printdata is a multi-level nested json object, it cannot be placed in a hidden field ...

I hope someone has some other idea or find a mistake!

+5
source share
1 answer

Add params responseType, header and cache to $ http as follows:

 $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
source

Source: https://habr.com/ru/post/1207111/


All Articles