TuffRivers's avatar

Best way to implement "download" reports feature with pagination

Hi All,

I have a basic CRUD app that has a reporting page allowing users to splice data and print reports based on basic filters (date, customer, transaction type).

My database handles filtering/pagination/search. Pagination needed as browser could be tasked to load 5000-6000 records for a single client if required by user.

What is best way to implement a "Download" feature so that user can download their paginated report in either CSV or PDF? I assume CSV is pretty straightforward as download link can execute a call for all records database serves up in json and app can convert to csv and offer download link. Would that be the same for PDF as well as long as it has pre formatting logic for the PDF?

Thanks!

0 likes
3 replies
lostdreamer_nl's avatar
Level 53

I would probably setup the backend endpoint to prepare the query, and then pass it on to an outputAdapter. I'd make 3 of these:

  • PaginatedOutputAdapter
  • CsvOutputAdapter
  • PdfOutputAdapter

They all accept the aforementioned query object, but they output differently.

  • The paginated one would return the paginated query object as you are doing now, so it will be used for showing the report to the user in the browser.
  • The CSV one will run the unpaginated query, create a csv file with it and the needed headers to force the download
  • PdfOutput does the same as Csv, but would use a template + laravel-dompdf to output a PDF for downloading.
1 like
TuffRivers's avatar

@lostdreamer_nl is outputadapter just a function i would create in my controller?

ie

public function PaginatedOutputAdapter {

some code that  gets paginated results

}

public function CsvOutputAdapter {

some code that  gets csv output for download

}

public function PdfOutputAdapter {

some code that  gets pdf results

}
lostdreamer_nl's avatar

If you want you could do it in the controller, it'd be a bit 'better' to separate them into different classes but for small projects it's pretty much ok to create 3 different methods that do the work.

Please or to participate in this conversation.