HTML to PDF Converter

HTML to PDF Converter

Convert HTML code or HTML files to PDF documents

📝 HTML Input

Quick Samples:

⚙️ PDF Settings

Converting HTML to PDF...

✅ Your PDF is ready!

Click the button below to download your PDF document

👁️ Preview

`; break; case 'table': sampleHtml = `Table Document

Data Table Example

NameEmailRole
John Doejohn@example.comAdministrator
Jane Smithjane@example.comEditor
Bob Johnsonbob@example.comContributor
`; break; case 'styled': sampleHtml = `Styled Document

Styled HTML Document

This document demonstrates various styling options that can be converted to PDF.

Special Highlight Section

This section uses gradients and custom styling that will be preserved in the PDF.

Features List

  • Beautiful gradient backgrounds
  • Custom list styling
  • Box shadows and rounded corners
  • Responsive design elements
`; break; case 'clear': sampleHtml = ''; break; } htmlInput.value = sampleHtml; currentHtmlContent = sampleHtml; updatePreview(); } function formatFileSize(bytes) { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } function clearAll() { htmlInput.value = ''; fileInput.value = ''; currentHtmlContent = ''; currentFileName = ''; fileInfo.style.display = 'none'; resultSection.style.display = 'none'; updatePreview(); } async function convertToPDF() { if (!currentHtmlContent) { alert('Please provide HTML content to convert.'); return; } // Show progress progressContainer.style.display = 'block'; progressBar.style.width = '0%'; progressText.textContent = 'Converting HTML to PDF...'; convertButton.disabled = true; try { // Update progress progressBar.style.width = '30%'; progressText.textContent = 'Rendering HTML content...'; // Use html2canvas to capture the preview container const canvas = await html2canvas(previewContainer, { scale: 2, useCORS: true, allowTaint: true, backgroundColor: includeBackground.checked ? '#ffffff' : '#ffffff', logging: false }); // Update progress progressBar.style.width = '70%'; progressText.textContent = 'Creating PDF document...'; // Create PDF const pdf = new jsPDF({ orientation: pageOrientation.value, unit: 'mm', format: pageSize.value }); const margin = parseInt(marginInput.value); const pageWidth = pdf.internal.pageSize.getWidth() - (margin * 2); const pageHeight = pdf.internal.pageSize.getHeight() - (margin * 2); // Calculate image dimensions to fit the page const imgWidth = pageWidth; const imgHeight = (canvas.height * pageWidth) / canvas.width; // Add image to PDF const imgData = canvas.toDataURL('image/jpeg', 0.9); pdf.addImage(imgData, 'JPEG', margin, margin, imgWidth, imgHeight); // Update progress progressBar.style.width = '100%'; progressText.textContent = 'PDF created successfully!'; // Generate PDF blob const pdfBlob = pdf.output('blob'); // Create download URL const url = URL.createObjectURL(pdfBlob); downloadButton.onclick = function() { const a = document.createElement('a'); a.href = url; a.download = currentFileName ? currentFileName.replace('.html', '.pdf').replace('.htm', '.pdf') : 'converted-document.pdf'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; // Show result after a short delay setTimeout(() => { progressContainer.style.display = 'none'; resultSection.style.display = 'block'; convertButton.disabled = false; // Scroll to result resultSection.scrollIntoView({ behavior: 'smooth' }); }, 500); } catch (error) { console.error('Error converting to PDF:', error); progressText.textContent = 'Error converting HTML to PDF. Please try again.'; convertButton.disabled = false; setTimeout(() => { progressContainer.style.display = 'none'; }, 2000); alert('Error converting HTML to PDF. Please check your HTML code and try again.'); } } function downloadPDF() { // This is handled in the convertToPDF function } });