Skip to content
Advertisement

Downloading generated excel only works locally | WordPress

I have a word press page that shows a report as an html table, on button click, that report should be downloaded as xlsx file.

The problem is that while download feature works perfectly on my local machine, on the server I get gibberish like this echoed on the screen, instead of file downloading:

PKǂ�RG�D�Z�[Content_Types].xml���N …

Here is the relevant code:

  • page-export.php

    JavaScript
  • functions.php

    JavaScript

I’ve tried adding additional headers I’ve found on similar questions, toggling ob_end_clean(), exit function after outputXlsx… etc. but It still only works locally.

Thanks in advance!

Advertisement

Answer

I finally fixed it!

Enabling debug mode in wordpress gave me the clues to solution with these two warnings:

  • Warning: Cannot modify header information – headers already sent
  • ob_end_clean(): failed to delete buffer. No buffer to delete

It turns out that all I had to do was to place the code for excel generation at the top of the page before html and add ob_start().

page-export.php:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement