From 4170ec35714a9a4fa8cc7143e38da9dfbc861102 Mon Sep 17 00:00:00 2001 From: Dannon Baker Date: Fri, 15 Oct 2021 21:27:49 -0400 Subject: [PATCH] Fix wonky scaling math for image embed into pdf, drop big header comments since it's just three methods now. --- .../chart/components/screenshot.js | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/client/src/mvc/visualization/chart/components/screenshot.js b/client/src/mvc/visualization/chart/components/screenshot.js index a449c281948..c352d24580c 100644 --- a/client/src/mvc/visualization/chart/components/screenshot.js +++ b/client/src/mvc/visualization/chart/components/screenshot.js @@ -10,20 +10,32 @@ export function createPNG(options) { }); } -/** SVG export */ export function createSVG(options) { domtoimage.toSvg(options.$el[0]).then(function (dataUrl) { saveAs(dataUrl, `${options.title || "my-chart"}.svg`); }); } -/** PDF export */ export function createPDF(options) { domtoimage.toPng(options.$el[0]).then(function (dataUrl) { const doc = new jsPDF(); - const w = doc.internal.pageSize.getWidth(); - const h = doc.internal.pageSize.getHeight(); - doc.addImage(dataUrl, "png", w * 0.1, h * 0.1, w * 0.8, h * 0.8); + // Calculate scaled image size. We want to leave a small margin, but + // fill the page width otherwise. + const pageWidth = doc.internal.pageSize.getWidth(); + const pageHeight = doc.internal.pageSize.getHeight(); + const elementWidth = options.$el.width(); + const elementHeight = options.$el.height(); + const scale = Math.min(pageWidth / elementWidth, pageHeight / elementHeight) * 0.95; + + doc.addImage( + dataUrl, + "png", + pageWidth * 0.025, + pageHeight * 0.025, + scale * elementWidth, + scale * elementHeight + ); + doc.save(`${options.title || "my-chart"}.pdf`); }); }