function convertHtmlToPdf() {
const element = document.getElementById('incidentReport');
const headerContent = generateHeaderContent(); // Get the header content
var reportPageHeight = $('#incidentReport').innerHeight();
var reportPageWidth = $('#incidentReport').innerWidth();
var pdf = new html2pdf(element, {
margin: [45, 10, 10, 10],
pagebreak: { mode: 'avoid-all' },
html2canvas: { scale: 1 },
image: { type: 'png', quality: 1.0 },
jsPDF: {
unit: 'pt',
format: [reportPageWidth, reportPageHeight],
orientation: 'landscape',
}
}).set(headerContent).save('IncidentReport.pdf');
}
function generateHeaderContent() {
const startDate = $('#startDateIncident').val();
const endDate = $('#finDateIncident').val();
const todayChecked = $('#todayIncident').is(':checked');
const dateRange = todayChecked ? 'Today' : (startDate + ' to ' + endDate);
const header = ` <div style="display: flex; align-items: center; margin-bottom:20px;">
<div style="margin-left:40px;">
<img src="/app-assets/images/logo/logo-color3.png" alt="Logo" style="max-height: 40px;">
</div>
<div style="font-size: 12px; margin-left:180px;">Date: ${dateRange}</div>
</div> `;
return header;
}
Troubleshooting Header Absence in ASP.NET’s html2pdf Generated PDF
I attempted to include a header in a PDF generated using html2pdf in ASP.NET. I expected the header to be visible in the generated PDF document, but despite setting it up correctly, it did not appear.