html2pdf.js icon indicating copy to clipboard operation
html2pdf.js copied to clipboard

Different Styles in PDF

Open Vaib215 opened this issue 1 year ago • 2 comments

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

Vaib215 avatar Aug 07 '23 16:08 Vaib215

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

umaraziz0 avatar Aug 08 '23 17:08 umaraziz0

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

ch3nx1 avatar Aug 09 '23 08:08 ch3nx1