From 880037c81e2accf3ad1ec5766881d9bab250ea2f Mon Sep 17 00:00:00 2001 From: Joyal Raphel Date: Mon, 3 Feb 2025 14:18:59 +0530 Subject: [PATCH 1/2] Added custom id to iframe body --- src/types/UseReactToPrintOptions.ts | 2 ++ src/utils/handlePrintWindowOnLoad.ts | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/types/UseReactToPrintOptions.ts b/src/types/UseReactToPrintOptions.ts index 1c708e6..699a403 100644 --- a/src/types/UseReactToPrintOptions.ts +++ b/src/types/UseReactToPrintOptions.ts @@ -58,4 +58,6 @@ export interface UseReactToPrintOptions { suppressErrors?: boolean; /** When passed, shadow root content will be copied */ copyShadowRoots?: boolean; + /** Add custom id to iframe body id */ + iframeBodyId?: string; } diff --git a/src/utils/handlePrintWindowOnLoad.ts b/src/utils/handlePrintWindowOnLoad.ts index 87629f7..547f128 100644 --- a/src/utils/handlePrintWindowOnLoad.ts +++ b/src/utils/handlePrintWindowOnLoad.ts @@ -1,7 +1,7 @@ -import {logMessages} from "./logMessage"; -import {startPrint} from "./startPrint"; -import {Font} from "../types/font"; -import type {UseReactToPrintOptions} from "../types/UseReactToPrintOptions"; +import { logMessages } from "./logMessage"; +import { startPrint } from "./startPrint"; +import { Font } from "../types/font"; +import type { UseReactToPrintOptions } from "../types/UseReactToPrintOptions"; import { cloneShadowRoots } from "./clone"; import { getErrorFromUnknown } from "./getErrorMessage"; @@ -62,7 +62,8 @@ export function handlePrintWindowOnLoad( pageStyle, nonce, suppressErrors, - copyShadowRoots + copyShadowRoots, + iframeBodyId } = options; // Some agents, such as IE11 and Enzyme (as of 2 Jun 2020) continuously call the @@ -72,6 +73,9 @@ export function handlePrintWindowOnLoad( const domDoc = printWindow.contentDocument ?? printWindow.contentWindow?.document; if (domDoc) { + if (iframeBodyId) { + domDoc.body.id = iframeBodyId; + } const appendedContentNode = domDoc.body.appendChild(clonedContentNode); if (copyShadowRoots) { cloneShadowRoots(contentNode, appendedContentNode, !!suppressErrors); @@ -83,7 +87,7 @@ export function handlePrintWindowOnLoad( const fontFace = new FontFace( font.family, font.source, - {weight: font.weight, style: font.style} + { weight: font.weight, style: font.style } ); printWindow.contentDocument!.fonts.add(fontFace); fontFace.loaded @@ -191,7 +195,7 @@ export function handlePrintWindowOnLoad( } else if (!videoNode.src) { // There are scenarios where `src` can be empty, for example with lazy loading. markLoaded(videoNode, ["Error loading video, `src` is empty", videoNode]); - } else{ + } else { videoNode.onloadeddata = () => { markLoaded(videoNode) }; From 1d20a0c49e124bdc91c2175b84aae558d7ecb04a Mon Sep 17 00:00:00 2001 From: Joyal Raphel Date: Mon, 3 Feb 2025 14:31:21 +0530 Subject: [PATCH 2/2] updated Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e8eee97..33c3d49 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ It is also possible to lazy set the ref if your content being printed is dynamic | **`preserveAfterPrint`** | `boolean` | Preserve the print iframe after printing. This can be useful for debugging by inspecting the print iframe | | **`print`** | `(iframe: HTMLIFrameElement) => Promise` | If passed, this function will be used instead of `window.print` to print the content. Use this to print in non-browser environments such as Electron | | **`suppressErrors`** | `boolean` | When passed, prevents `console` logging of errors | +| **`iframeBodyId`** | `string` | Set custom id to iframe body | ## Compatibility