import html2pdf from "html2pdf.js";

// jsPDF config doc
// https://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html
export const jsPDFConfigs = {
  A3: { unit: "mm", format: "a3", orientation: "landscape" },
  A4: { unit: "mm", format: "a4", orientation: "portrait" },
  "8K": {
    unit: "mm",
    format: [370, 255],
    orientation: "landscape",
  },
};

export async function buildPdf(
  { element, pageSize, filename },
  returnBlob = false
) {
  const opt = {
    margin: 0,
    html2canvas: { scale: 6 },
    filename: filename || "",
    jsPDF: {
      ...jsPDFConfigs[pageSize],
    },
  };

  if (returnBlob) {
    return await html2pdf().set(opt).from(element).outputPdf("blob");
  } else {
    await html2pdf().set(opt).from(element).save();
  }
}
