Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(export): clamp step screenshots so they cannot overflow a pdf page

Luis Ortizeff608eb4285
diff --git a/src/core/export/__tests__/utils.test.ts b/src/core/export/__tests__/utils.test.ts
index a3197a9..c45884c 100644
--- a/src/core/export/__tests__/utils.test.ts
+++ b/src/core/export/__tests__/utils.test.ts
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest';
-import { blobToBase64, blobToDataUrl, escapeHtml, extractDomain, formatDate } from '@/core/export/utils';
+import { blobToBase64, blobToDataUrl, escapeHtml, extractDomain, fitImage, formatDate } from '@/core/export/utils';
import type { Step } from '@/core/guides/types';
function makeStep(overrides: Partial<Step> = {}): Step {
@@ -118,3 +118,34 @@ describe('blobToDataUrl', () => {
expect(atob(b64Part)).toBe('hello');
});
});
+
+describe('fitImage', () => {
+ it('leaves an image that already fits untouched', () => {
+ expect(fitImage(103, 58, 211.5)).toEqual({ width: 103, height: 58 });
+ });
+
+ it('scales width down proportionally when the height exceeds the cap', () => {
+ const fitted = fitImage(143, 300, 211.5);
+ expect(fitted.height).toBe(211.5);
+ expect(fitted.width).toBeCloseTo(143 * (211.5 / 300), 5);
+ expect(fitted.width / fitted.height).toBeCloseTo(143 / 300, 5);
+ });
+
+ it('keeps a portrait phone screenshot inside the page at medium scale', () => {
+ const width = 103;
+ const height = (2436 / 1125) * width;
+ const fitted = fitImage(width, height, 211.5);
+ expect(height).toBeGreaterThan(211.5);
+ expect(fitted.height).toBe(211.5);
+ expect(fitted.width).toBeLessThan(width);
+ });
+
+ it('returns the input for a zero or non-finite height', () => {
+ expect(fitImage(100, 0, 200)).toEqual({ width: 100, height: 0 });
+ expect(fitImage(100, Number.NaN, 200)).toEqual({ width: 100, height: Number.NaN });
+ });
+
+ it('leaves an image exactly at the cap untouched', () => {
+ expect(fitImage(143, 211.5, 211.5)).toEqual({ width: 143, height: 211.5 });
+ });
+});
diff --git a/src/core/export/pdf-export.ts b/src/core/export/pdf-export.ts
index 6a558fc..8202eb8 100644
--- a/src/core/export/pdf-export.ts
+++ b/src/core/export/pdf-export.ts
@@ -2,7 +2,7 @@ import { jsPDF } from 'jspdf';
import { i18n } from '#imports';
import { fitLogo, loadBranding } from '@/core/export/branding';
import { type ExportOptions, IMAGE_SCALE_FACTORS, loadExportOptions } from '@/core/export/options';
-import { blobToDataUrl, extractDomain, formatDate } from '@/core/export/utils';
+import { blobToDataUrl, extractDomain, fitImage, formatDate } from '@/core/export/utils';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
import { hexToRgb } from '@/core/screenshot/color';
import { resolveViewport } from '@/core/screenshot/geometry';
@@ -146,18 +146,26 @@ export async function exportGuideAsPDF(
const screenshot = opts.screenshots ? screenshots.get(step.id) : undefined;
let imgDataUrl: string | null = null;
let imgHeight = 0;
+ let stepImgWidth = imgWidth;
+ const textOverhead = 6 + descLines.length * 5 + 4;
if (screenshot) {
try {
const rendered = await renderScreenshot(screenshot, { format: 'image/jpeg', quality: JPEG_QUALITY });
imgDataUrl = await blobToDataUrl(rendered);
const viewport = resolveViewport(screenshot);
- imgHeight = (viewport.height / viewport.width) * imgWidth;
+ const fitted = fitImage(
+ imgWidth,
+ (viewport.height / viewport.width) * imgWidth,
+ FOOTER_Y - 8 - STEP_TOP - textOverhead,
+ );
+ stepImgWidth = fitted.width;
+ imgHeight = fitted.height;
} catch (err) {
logger.warn('PDF: failed to load screenshot for step', step.index, err);
}
}
- const blockH = 6 + descLines.length * 5 + 4 + imgHeight;
+ const blockH = textOverhead + imgHeight;
if (sy + blockH > FOOTER_Y - 8 && sy > STEP_TOP) {
sy = startStepPage();
}
@@ -204,9 +212,9 @@ export async function exportGuideAsPDF(
let iy = uy + 4;
if (imgDataUrl) {
- doc.addImage(imgDataUrl, 'JPEG', TEXT_X, iy, imgWidth, imgHeight);
+ doc.addImage(imgDataUrl, 'JPEG', TEXT_X, iy, stepImgWidth, imgHeight);
const altText = screenshot?.edits?.alt || i18n.t('export.stepLabel', [stepNum]);
- doc.text(doc.splitTextToSize(altText, imgWidth), TEXT_X, iy + 4, { renderingMode: 'invisible' });
+ doc.text(doc.splitTextToSize(altText, stepImgWidth), TEXT_X, iy + 4, { renderingMode: 'invisible' });
iy += imgHeight;
}
sy = iy + STEP_GAP;
diff --git a/src/core/export/utils.ts b/src/core/export/utils.ts
index cef2c01..f4985ef 100644
--- a/src/core/export/utils.ts
+++ b/src/core/export/utils.ts
@@ -66,3 +66,8 @@ export async function fetchFaviconBase64(domain: string): Promise<string | null>
return null;
}
}
+
+export function fitImage(width: number, height: number, maxHeight: number): { width: number; height: number } {
+ if (!Number.isFinite(height) || height <= 0 || height <= maxHeight) return { width, height };
+ return { width: width * (maxHeight / height), height: maxHeight };
+}