127 lines
4.1 KiB
JavaScript
127 lines
4.1 KiB
JavaScript
/*******************************************************************
|
|
|
|
Module : /assets/js/pdf.js
|
|
Desc. : JS - Cetak Laporan PDF
|
|
Created By : Siau Pheng ([email protected]).
|
|
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 <[email protected]>
|
|
*
|
|
* @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 = `
|
|
<div class="modal fade" id="winAPPPDF" tabindex="-1" data-bs-backdrop="static" aria-labelledby="winAPPPDFLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered modal-xl" style="height: 90vh;">
|
|
<div class="modal-content h-100">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="winAPPPDFLabel">${filename}</h5>
|
|
<div class="ms-auto d-flex align-items-center gap-2">
|
|
<a href="${objectUrl}" download="${filename}" class="btn btn-sm btn-outline-primary">
|
|
<i class="bi bi-download"></i> Download
|
|
</a>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
</div>
|
|
<div class="modal-body p-0 h-100">
|
|
<object data="${pdfSrc}" type="application/pdf" class="w-100 h-100">
|
|
<iframe src="${pdfSrc}" class="w-100 h-100" style="border: none;">
|
|
<p class="p-3">Ada kendala dalam Preview PDF di browser Anda.
|
|
<a href="${objectUrl}" download="${filename}">Unduh ${filename}</a> instead.
|
|
</p>
|
|
</iframe>
|
|
</object>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
$('#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));
|
|
}
|
|
}
|