eKoopmans / html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.
MIT License
4.07k stars 1.38k forks source link

Different Styles in PDF #659

Open Vaib215 opened 1 year ago

Vaib215 commented 1 year ago

I getting different styles in Original & PDF. Please tell me urgent solution or workaround.

These are the config I am using:

const ResumePDFButton: React.FC<ResumePDFButtonProps> = ({ resumeRef }) => {
  const downloadPDF = () => {
    const element = resumeRef.current;

    if (!element) return;

    const options = {
      margin: [8, 16, 16, 0],
      filename: "resume.pdf",
      image: { type: "jpeg", quality: 0.98 },
      html2canvas: { scale: 2, letterRendering: true },
      jsPDF: {
        unit: "pt",
        format: "letter",
        orientation: "portrait",
      },
      pagebreak: { mode: ["avoid-all", "css", "legacy"] },
    };

    // @ts-ignore
    import("html2pdf.js").then((html2pdf) => {
      const pdf = html2pdf.default();
      pdf
        .from(element)
        .set(options)
        .outputPdf("datauristring")
        .then((data: string) => {
          const link = document.createElement("a");
          link.href = data;
          link.download = "resume.pdf";
          link.click();
          link.remove();
        });
    });
  };

  return (
    <button onClick={downloadPDF} className="bg-blue-600 text-white p-2">
      Download PDF
    </button>
  );
};

Original image

PDF image

umaraziz0 commented 1 year ago

Can confirm, I'm having the same issue. Using TailwindCSS for my styling.

ch3nx1 commented 1 year ago

maybe it's caused by tailwind, I resolved it through set @layer base { img { display: initial; } } in main.css you can find more discussions in here: https://github.com/niklasvh/html2canvas/issues/2775#issuecomment-1204988157