jQuery(document).ready(function($) {
const table = $('#bannerTable').DataTable({
dom: 'Blfrtip',
paging: true, // Enable pagination
pageLength: 10, // Default number of rows per page
lengthMenu: [ [10, 25, 50, -1], [10, 25, 50, "All"] ],
buttons: [{
extend: 'excelHtml5',
title: 'Banner Data',
text: ' Excel',
className: 'button button-secondary'
},
{
extend: 'pdfHtml5',
title: 'Banner Data',
text: ' PDF',
orientation: 'landscape',
pageSize: 'A4',
className: 'button button-secondary'
}
]
});
function populateFilters(columnIndex, selector) {
const column = table.column(columnIndex);
const values = new Set();
column.data().each(function(d) {
d.split(',').forEach(item => values.add(item.trim()));
});
Array.from(values).sort().forEach(function (val) {
if (val) {
$(selector).append(`${val}`);
}
});
}
// Column indices: Media(2), Type(3), Availability(6), District(7), Place(8)
populateFilters(2, '#filterMedia');
populateFilters(3, '#filterType');
populateFilters(6, '#filterAvailability');
populateFilters(7, '#filterDistrict');
// populateFilters(8, '#filterPlace');
$('#filterMedia, #filterType, #filterAvailability, #filterDistrict, #filterPlace').on('change', function () {
table.draw(); // 4) {
// alert("You can only generate proposals for up to 4 banners at a time.");
// return;
// }
const $button = $(this).prop('disabled', true).text('Generating...');
const $pdfContainer = $('#pdfContainer').empty().css('display', 'block');
let page = 1;
const coverHTML = `

`;
$pdfContainer.append(coverHTML);
selectedRows.each(function() {
const row = $(this);
const title = row.find('td:eq(1)').text();
const media = row.find('td:eq(2)').text();
const type = row.find('td:eq(3)').text();
const size = row.find('td:eq(4)').text();
const district = row.find('td:eq(7)').text();
const place = row.find('td:eq(8)').text();
const imagesArray = row.find('.banner-images img');
const faciav=row.find('td:eq(9)').text();
// console.log(faciav,'Hi')
let images = '';
imagesArray.each(function() {
const src = $(this).attr('src');
if (src) {
images += `
`;
}
});
if (images.trim() === '' && !title.trim() && !media.trim() && !type.trim() && !size.trim()) return; // Skip if no content
const html = `
${title}, ${district}

${images}
Facia: ${faciav}
Size: ${size}
Type: ${type}
Page ${page++}
`;
$pdfContainer.append(html);
});
// Ensure last page doesn't have page break
$pdfContainer.find('.proposal-page').last().css('page-break-after', 'auto');
// Remove empty proposal pages if any
$pdfContainer.find('.proposal-page').each(function() {
if (!$(this).text().trim()) {
$(this).remove(); // Remove empty page
}
});
setTimeout(() => {
html2pdf().set({
margin: [0, 0, 0, 0],
filename: `banner-proposal-${Date.now()}.pdf`,
image: {
type: 'jpeg',
quality: 0.98
},
html2canvas: {
scale: 2,
useCORS: true,
allowTaint: false,
logging: true,
scrollX: 0,
scrollY: 0
},
jsPDF: {
unit: 'mm',
format: 'a4',
orientation: 'portrait',
putOnlyUsedFonts: true
}
}).from($pdfContainer[0]).save().then(() => {
$pdfContainer.css('display', 'none');
$button.prop('disabled', false).text('Create Proposal PDF');
});
}, 500);
});
});
Подробнее здесь: https://stackoverflow.com/questions/795 ... om-pdf-wit
Мобильная версия