sandrods / odf-report

Generates ODF files, given a template (.odt) and data, replacing tags

Home Page:http://sandrods.github.com/odf-report

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Open file instead of downloading it

thalesmiguel opened this issue · comments

First of all, thank you for this awesome gem!

I'd like to know if it's possible to automatically open de generated .odt file instead of downloading it when the print action is called.
In this scenario, the user would click on the print button and libreoffice would open with the generated file.
As of today, the user clicks on the print button, downloads the generated file, finds it and then opens it.

Thank you in advance.

I think I've found something!
As it is explained in the README file, the code used to generate the report is this:

def print
  .
  .
  .
  send_data report.generate, type: 'application/vnd.oasis.opendocument.text',
                              disposition: 'attachment',
                              filename: 'report.odt'
end

In my case, I need to open the generated file as soon as it is generated (no need to save it). To do so, I used this:

def print
  .
  .
  .
  path = "#{Rails.root}/app/reports/generated/report.odt"
  report.generate(path)
  `libreoffice #{path}`
  `rm -rf #{path}`
end

Explaining

First, I save the path to the file that will be generated (I've also created a folder in /app/reports called generated to organize the reports);
Then, I generate the report using report.generate(path);
To open this file on libreoffice, I'm calling `libreoffice #{path}`. It opens LibreOffice with the generated file.
To wrap it up, I call `rm -rf #{path}` to delete the report.

Notes

  1. Code on rb files inside `` is executed as command lines;
  2. This print action is inside my controller and there is a PUT route to it.