/web_pdf_preview/static/src/js/web_pdf_preview.js

https://gitlab.com/darmawan.fatria/provjabar-upkp-addons · JavaScript · 125 lines · 90 code · 11 blank · 24 comment · 26 complexity · aeb658bf68fa0f34ded1ada377c9a343 MD5 · raw file

  1. /*############################################################################
  2. # Web PDF Report Preview & Print
  3. # Copyright 2014 wangbuke <wangbuke@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ############################################################################*/
  19. openerp.web_pdf_preview = function(instance) {
  20. var wkhtmltopdf_state;
  21. var trigger_download = function(self, response, c) {
  22. var params = {
  23. data: JSON.stringify(response),
  24. token: new Date().getTime()
  25. };
  26. var url = self.session.url('/report/preview', params);
  27. //window.open('/report/preview?data=' + encodeURIComponent(JSON.stringify(response)) + '&token=' + new Date().getTime() + '&session_id=' + self.session.session_id, 'report', '');
  28. window.open(url, 'report', '');
  29. instance.web.unblockUI();
  30. return;
  31. }
  32. instance.web.ActionManager = instance.web.ActionManager.extend({
  33. ir_actions_report_xml: function(action, options) {
  34. var self = this;
  35. instance.web.blockUI();
  36. action = _.clone(action);
  37. _t = instance.web._t;
  38. // QWeb reports
  39. if ('report_type' in action && (action.report_type == 'qweb-html' || action.report_type == 'qweb-pdf' || action.report_type == 'controller')) {
  40. var report_url = '';
  41. switch (action.report_type) {
  42. case 'qweb-html':
  43. report_url = '/report/html/' + action.report_name;
  44. break;
  45. case 'qweb-pdf':
  46. report_url = '/report/pdf/' + action.report_name;
  47. break;
  48. case 'controller':
  49. report_url = action.report_file;
  50. break;
  51. default:
  52. report_url = '/report/html/' + action.report_name;
  53. break;
  54. }
  55. // generic report: no query string
  56. // particular: query string of action.data.form and context
  57. if (!('data' in action) || !(action.data)) {
  58. if ('active_ids' in action.context) {
  59. report_url += "/" + action.context.active_ids.join(',');
  60. }
  61. } else {
  62. report_url += "?options=" + encodeURIComponent(JSON.stringify(action.data));
  63. report_url += "&context=" + encodeURIComponent(JSON.stringify(action.context));
  64. }
  65. var response = new Array();
  66. response[0] = report_url;
  67. response[1] = action.report_type;
  68. var c = openerp.webclient.crashmanager;
  69. if (action.report_type == 'qweb-html') {
  70. window.open(report_url, '_blank', 'scrollbars=1,height=900,width=1280');
  71. instance.web.unblockUI();
  72. } else if (action.report_type === 'qweb-pdf') {
  73. // Trigger the download of the pdf/controller report
  74. (wkhtmltopdf_state = wkhtmltopdf_state || openerp.session.rpc('/report/check_wkhtmltopdf')).then(function (presence) {
  75. // Fallback on html if wkhtmltopdf is not installed or if OpenERP is started with one worker
  76. if (presence === 'install') {
  77. self.do_notify(_t('Report'), _t('Unable to find Wkhtmltopdf on this \
  78. system. The report will be shown in html.<br><br><a href="http://wkhtmltopdf.org/" target="_blank">\
  79. wkhtmltopdf.org</a>'), true);
  80. report_url = report_url.substring(12)
  81. window.open('/report/html/' + report_url, '_blank', 'height=768,width=1024');
  82. instance.web.unblockUI();
  83. return;
  84. } else if (presence === 'workers') {
  85. self.do_notify(_t('Report'), _t('You need to start OpenERP with at least two \
  86. workers to print a pdf version of the reports.'), true);
  87. report_url = report_url.substring(12)
  88. window.open('/report/html/' + report_url, '_blank', 'height=768,width=1024');
  89. instance.web.unblockUI();
  90. return;
  91. } else if (presence === 'upgrade') {
  92. self.do_notify(_t('Report'), _t('You should upgrade your version of\
  93. Wkhtmltopdf to at least 0.12.0 in order to get a correct display of headers and footers as well as\
  94. support for table-breaking between pages.<br><br><a href="http://wkhtmltopdf.org/" \
  95. target="_blank">wkhtmltopdf.org</a>'), true);
  96. }
  97. return trigger_download(self, response, c);
  98. });
  99. } else if (action.report_type === 'controller') {
  100. return trigger_download(self, response, c);
  101. }
  102. } else {
  103. var params = {
  104. action: JSON.stringify(action),
  105. token: new Date().getTime()
  106. };
  107. var url = self.session.url('/web/report', params);
  108. window.open(url, 'report', '');
  109. instance.web.unblockUI();
  110. return;
  111. }
  112. },
  113. });
  114. };