Printing a PDF seems pretty straight forward at first: I should just be able to run the window.print JavaScript function and be all set. Of course, it isn't actually that simple because then I wouldn't be writing this post.
Our project has a requirement to support Chrome, Firefox, Safari, and Edge so I needed a solution that would work in all browsers. After a bit of digging I found that I'd need to do a little bit of dancing around Edge (big shocker there), but the other three would readily support the method I wanted to use. The route that made the most sense was to use the built-in (mostly) createObjectUrl function to build a URL from a Blob created from the PDF bytes, then open that URL in a new window. That was easy enough:
1: print(): void {
2: const url = URL.createObjectUrl(new Blob([this.reportBytes], { type: 'application/pdf' }));
3: window.open(url);
4: }
As I wrote before, this worked fine for most of the browsers, but Edge wasn't happy so I had to modify it to work:
1: print(): void {
2: if (window.navigator && window.navigator.msSaveOrOpenBlob) {
3: window.navigator.msSaveOrOpenBlobBlob([this.reportBytes]));
4: } else {
5: const url = URL.createObjectUrl(new Blob([this.reportBytes], { type: 'application/pdf' }));
6: window.open(url);
7: }
4: }
This isn't a perfect solution because the report doesn't actually print right away when you click the Print button. It doesn't even invoke the native print function of the browser. It does, however, open the PDF in a new tab and allow the user to print it if they wish. We had to go that route because Edge had some issues launching the print dialog properly with a PDF (which goes back to not being able to use the createObjectUrl function). Not perfect, but good enough.
Display a list of all reports in SSRSAllow the user to click on a single report to view the reportAllow the user to print the report- Allow the user to download the report
- SSRS Reports in an Angular App (Part 1 - Showing the Report)
- SSRS Reports in an Angular App (Part 2 - Showing the Report (correctly))
- SSRS Reports in an Angular App (Part 3 - Printing the Report)(this post)
- SSRS Reports in an Angular App (Part 4 - Downloading the Report)
No comments:
Post a Comment