Guest User

Untitled

a guest
Apr 17th, 2025
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.43 KB | Source Code | 0 0
  1. const script = document.createElement('script');
  2. script.src = 'https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js';
  3. script.onload = () => {
  4.   const element = document.querySelector('.problemindexholder');
  5.   if (!element) {
  6.     alert("Element with class 'problemindexholder' not found.");
  7.     return;
  8.   }
  9.  
  10.   const rawTitle = element.querySelector('.title')?.innerText || 'problem';
  11.   const titleParts = rawTitle.split('. ');
  12.   const cleanTitle = titleParts.length > 1 ? titleParts[1] : rawTitle;
  13.   const safeFilename = cleanTitle.replace(/[<>:"/\\|?*\x00-\x1F]/g, '').trim() + '.pdf';
  14.  
  15.  const clone = element.cloneNode(true);
  16.  clone.style.margin = '0 auto';
  17.  clone.style.width = 'fit-content';
  18.  clone.style.maxWidth = '90vw';
  19.  
  20.  const tempContainer = document.createElement('div');
  21.  tempContainer.style.padding = '20px';
  22.  tempContainer.style.background = 'white';
  23.  tempContainer.appendChild(clone);
  24.  document.body.appendChild(tempContainer);
  25.  
  26.  html2pdf().set({
  27.    margin:       0,
  28.    filename:     safeFilename,
  29.    image:        { type: 'jpeg', quality: 1 },
  30.    html2canvas:  { scale: 2, useCORS: true, scrollY: 0 },
  31.    jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' },
  32.    pagebreak:    { mode: ['avoid-all', 'css', 'legacy'] }
  33.  }).from(tempContainer).save().then(() => {
  34.    document.body.removeChild(tempContainer);
  35.  });
  36. };
  37. document.body.appendChild(script);
  38.  
Tags: JavaScript
Advertisement
Add Comment
Please, Sign In to add comment