Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const script = document.createElement('script');
- script.src = 'https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js';
- script.onload = () => {
- const element = document.querySelector('.problemindexholder');
- if (!element) {
- alert("Element with class 'problemindexholder' not found.");
- return;
- }
- const rawTitle = element.querySelector('.title')?.innerText || 'problem';
- const titleParts = rawTitle.split('. ');
- const cleanTitle = titleParts.length > 1 ? titleParts[1] : rawTitle;
- const safeFilename = cleanTitle.replace(/[<>:"/\\|?*\x00-\x1F]/g, '').trim() + '.pdf';
- const clone = element.cloneNode(true);
- clone.style.margin = '0 auto';
- clone.style.width = 'fit-content';
- clone.style.maxWidth = '90vw';
- const tempContainer = document.createElement('div');
- tempContainer.style.padding = '20px';
- tempContainer.style.background = 'white';
- tempContainer.appendChild(clone);
- document.body.appendChild(tempContainer);
- html2pdf().set({
- margin: 0,
- filename: safeFilename,
- image: { type: 'jpeg', quality: 1 },
- html2canvas: { scale: 2, useCORS: true, scrollY: 0 },
- jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
- pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
- }).from(tempContainer).save().then(() => {
- document.body.removeChild(tempContainer);
- });
- };
- document.body.appendChild(script);
Advertisement
Add Comment
Please, Sign In to add comment