Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture PDF inside iframe/object/embed #3187

Open
nishanth-cn opened this issue Aug 13, 2024 · 0 comments
Open

Capture PDF inside iframe/object/embed #3187

nishanth-cn opened this issue Aug 13, 2024 · 0 comments

Comments

@nishanth-cn
Copy link

nishanth-cn commented Aug 13, 2024

Hi ,
Thanks for the wonderful library.

I was just exploring it before using it for my project and i encountered a major issue.
Issue
Contents inside an <iframe> or object or embed doesn't capture if it is a pdf.

code

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Using an existing canvas to draw on</title>
    <style>
        canvas {
            border: 1px solid grey;
        }
        button {
            display: block;
        }
        #text {
            background: cyan;
            padding: 5px 10px;
			line-height: 1em;
			display:	block;
        }
    </style>
</head>
<body>
<div><h1>Html2canvas</h1>
    <div `>`
		<span id="text">Normal text</span>
		
	</div>
	<object id="frame" data="kids.pdf"  width="100%" height="400px"></object>
	<!--<iframe id="frame" data="kids.pdf" ></iframe>-->

	<h3>Captured data</h3>
	<canvas id="textcapture" width="100%" height="200"></canvas>
	<canvas id="iframecapture" width="500" height="200"></canvas>
</div>

<script type="text/javascript" src="html2canvas.js"></script>
<button>Capture</button>
<script type="text/javascript">
    var textcanvas = document.querySelector("#textcapture");
	var framecanvas = document.querySelector("#iframecapture");
    
    document.querySelector("button").addEventListener("click", function() {
		html2canvas(document.querySelector("#text")	, {canvas: textcanvas, scale: 1}).then(function(canvas) {
            console.log('Text captured');
        });
        html2canvas(document.querySelector("#frame").contentDocument.body, {canvas: framecanvas, scale: 1,useCORS:true}).then(function(canvas) {
            console.log('Iframe captured');
        });
    }, false);

</script>
</body>
</html>

details
Span element in the main document is captured fine. However, <iframe> fails if content is a pdf, even if it is in same domain
I have tried multiple was to get the document inside <iframe>, but it always fails.
I have tried SetTimeout after looking at couple of other issue but it didn't solve the problem.

is there any way to fix this or is it a limitation of html2canvas even if documents are in same domain ?.
It would be helpful , if you suggest other libraries which have solution for this.

Specifications:

  • html2canvas version tested with: 1.4.1
  • Browser & version: Chrome 127.0.6533.100
  • Operating system: Windows 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant