Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird spacing around texts. #722

Open
GourabPaul007 opened this issue Sep 6, 2024 · 2 comments
Open

Weird spacing around texts. #722

GourabPaul007 opened this issue Sep 6, 2024 · 2 comments

Comments

@GourabPaul007
Copy link

I am using react-to-print library in react to print a pdf, then html2pdf library in react to download the pdf. When i print using react-to-print, it prints correctly but when i use html2pdf to download the pdf in client side, the styling gets messed up. some elements are pushed upwards/downwards or added with spaces.
This is the printed image =>
printed image

This is the downloaded image =>
downloaded image

the code snippet of what i am using is =>

  const pdfOptions = {
    margin: 0,
    filename: "Resume.pdf",
    image: { type: "jpeg", quality: 0.98 },
    enableLinks: true,
    scale: 2,
    html2canvas: {
      scale: 4,
      useCORS: true,
    },
    jsPDF: { unit: "mm", format: "a4", orientation: "p", putOnlyUsedFonts: true, precision: 0 },
    pagebreak: { mode: ["avoid-all", "css", "legacy"] },
  };
  const handleDownload = useReactToPrint({
    content: () => componentRef.current,
    documentTitle: "Resume",
    copyStyles: true,
    print: async (printIframe: HTMLIFrameElement) => {
      const pdfdoc = printIframe.contentDocument;
      if (pdfdoc) {
        const html = pdfdoc.getElementsByTagName("body")[0];
        try {
          const html2pdf = (await import("html2pdf.js")).default;
          await html2pdf().set(pdfOptions).from(html).save();
        } catch (error) {
          console.error("Failed to load html2pdf.js", error);
        }
      }
    },
    onAfterPrint: () => {
      try {
        console.log("Downloaded");
      } catch (e) {
        console.error(e);
      }
    },
  });
@gregg-cbs
Copy link

I am seeing this too...

HTML SCREENSHOT:
image

PDF SCREENSHOT:
image

@Basnetsuman4
Copy link

Basnetsuman4 commented Dec 6, 2024

niklasvh/html2canvas#2775 (comment)
try this, worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants