How to Export Ag-Grid to PDF Format in Angular 2?

落爺英雄遲暮 提交于 2021-02-07 09:19:23

问题


I'm working on AG-GRID in Angular 2. I have found that there is an option only to export the grid into Excel.

How can I export the grid into PDF?


回答1:


Exporting to pdf whilst keeping the styling is a pretty tricky task and there are a few options out there to do what you desire.

  1. html2pdf: This converts the html to canvas then from canvas to an image and then from image to pdf. The problem is that this implementation tends to produce a low resolution image.

  2. jsPdf: This library is a js implementation and therefore does not include the page styling. Styles have to be added separately using the jsPdf and it is a bit limited so recreating the quite complex design generated by AG_GRID would require a lot of work.

  3. phantomjs: This is a headless bowser which Api provides screen capture capability. rastersize is an example of the implementation, this can be integrated into an app by creating a que for calls to phantomjs.




回答2:


You can print the grid data. Chrome gives you an option to save it as pdf. But, you might have other html elements in the page, which you probably don't want to be a part of the PDF (or, print). So:

  1. Render the grid in a separate page (You can transfer the grid state object to a separate component to render exactly the same grid view (Sort, filter etc).
  2. Call the grid function api.setDomLayout('print');
  3. Call window.print().
  4. Redirect him back to the previous page.

I have written an example code (Take it as an example only, tweak as per needs): https://stackblitz.com/edit/angular-ag-grid-pdf

You can play with the width to fill the page.



来源:https://stackoverflow.com/questions/46846593/how-to-export-ag-grid-to-pdf-format-in-angular-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!