/******************************************************************* Module : /assets/js/pdf.js Desc. : JS - Cetak Laporan PDF Created By : Siau Pheng (siaupheng@3fonia.com). Created Date : April 6th, 2015. Last Modified : November 2nd, 2026. (c) 3FONIA Software; WWW.3FONIA.COM. ******************************************************************* * * Class Pdf * * Preview dan download file pdf. * * @license MIT, https://www.3fonia.com * @copyright Copyright (c) 2008-2026, Siau Pheng * * @param {object} options * @param {string} options.url - Endpoint PDF File URL * @param {number} [options.zoom=80] - Zoom percentage in PDF viewer * @param {string} [options.filename='report.pdf'] - Default file name for download is header is missing * @param {Array} [options.params] - Request payload */ class Pdf { constructor(options = {}) { this.url = typeof options === 'string' ? options : options.url || ''; this.zoom = options.zoom || 80; this.filename = options.filename || 'report.pdf'; this.params = options.params || []; } addParam(id, val) { this.params.push(val !== undefined ? `${id}=${encodeURIComponent(val)}` : id); return this; } async downloadPdf() { if (!this.url) { console.warn('Url PDF dibutuhkan!'); return; } await $.ajax({ type: 'POST', url: this.url, data: FoniaJS.RequestParam(this.params), xhrFields: { responseType: 'blob' } }) .done((blob, status, xhr) => { let filename = this.filename; const disposition = xhr.getResponseHeader('Content-Disposition'); const match = disposition?.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/); if (match?.[1]) filename = match[1].replace(/['"]/g, ''); const url = URL.createObjectURL(blob); const link = Object.assign(document.createElement('a'), { href: url, download: filename }); link.click(); URL.revokeObjectURL(url); }) .fail((xhr, status, err) => console.error('Gagal proses download PDF karena : ', err)); } async showPdf() { if (!this.url) { console.warn('Url PDF dibutuhkan!'); return; } await $.ajax({ type: 'POST', url: this.url, data: FoniaJS.RequestParam(this.params), xhrFields: { responseType: 'blob' } }) .done((blob, status, xhr) => { let filename = this.filename; const disposition = xhr.getResponseHeader('Content-Disposition'); const match = disposition?.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/); if (match?.[1]) filename = match[1].replace(/['"]/g, ''); const objectUrl = URL.createObjectURL(blob); const pdfSrc = `${objectUrl}#zoom=${zoom}&page=1`; const modalHtml = ` `; $('#winAPPPDF').modal('dispose').remove(); $(modalHtml) .appendTo('body') //.on('shown.bs.modal', () => FoniaJS.FormDecorated()) .on('hidden.bs.modal', function () { URL.revokeObjectURL(objectUrl); $(this).remove(); }) .modal('show'); }) .fail((xhr, status, err) => console.error('Gagal proses preview PDF karena : ', err)); } }