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

Relative path in iframe #1461

Open
2 tasks done
shenzhuxi opened this issue Mar 8, 2018 · 3 comments · Fixed by yorickshan/html2canvas-pro#5 · May be fixed by #2961
Open
2 tasks done

Relative path in iframe #1461

shenzhuxi opened this issue Mar 8, 2018 · 3 comments · Fixed by yorickshan/html2canvas-pro#5 · May be fixed by #2961

Comments

@shenzhuxi
Copy link

shenzhuxi commented Mar 8, 2018

Please make sure you are testing with the latest release of html2canvas.
Old versions are not supported and issues reported for them will be closed.

Please follow the general troubleshooting steps first:

  • You are using the latest version
  • You are testing using the non-minified version of html2canvas and checked any potential issues reported in the console

Bug reports:

In my use case, I tried to use html2canvas in http://localhost/a.html to render b.html in <iframe src="http://localhost/dir/b.html"></iframe>.
Since all the paths in b.html are relative like "images/c.jpg", I got log like this

imageLoadHandler @ html2canvas.js:6790
addImage @ html2canvas.js:6818
loadImage @ html2canvas.js:6695
getImage @ html2canvas.js:858
NodeContainer @ html2canvas.js:772
parseNodeTree @ html2canvas.js:3621
parseNodeTree @ html2canvas.js:3650
parseNodeTree @ html2canvas.js:3650
NodeParser @ html2canvas.js:3596
(anonymous) @ html2canvas.js:3506
Promise.then (async)
(anonymous) @ html2canvas.js:3496
Promise.then (async)
renderElement @ html2canvas.js:3472
html2canvas @ html2canvas.js:3401

CSS file also can't be loaded because the path became 'http://localhost/' + 'css/d.css' rather than 'http://localhost/dir/css/d.css'.

If I use Javascript to convert the relative paths in iframe content into absolute before run html2canvas, the images can be rendered correctly and css files also can be loaded.

I found that in Clone.js iframeLoader seems alway assume a.html and b.html have the same baseURI. So I wonder is it the right place to add a better url hander for the render target.

Here is my quick and dirty fix:

diff --git a/src/Clone.js b/src/Clone.js
index 018e6dc..1fa22be 100644
--- a/src/Clone.js
+++ b/src/Clone.js
@@ -589,6 +589,15 @@ export const cloneWindow = (
     renderer: (element: HTMLElement, options: Options, logger: Logger) => Promise<*>
 ): Promise<[HTMLIFrameElement, HTMLElement, ResourceLoader]> => {
     const cloner = new DocumentCloner(referenceElement, options, logger, false, renderer);
+    var images = cloner.documentElement.getElementsByTagName('img');
+    var links = cloner.documentElement.getElementsByTagName('link');
+    var directory = cloner.documentElement.baseURI.split('/').slice(0, -1).join('/');
+    Array.from(images).forEach(function(ele){
+        ele.src = directory + "/" + ele.getAttribute("src");
+    });
+    Array.from(links).forEach(function(ele){
+        ele.href = directory + "/" + ele.getAttribute("href");
+    });
     const scrollX = ownerDocument.defaultView.pageXOffset;
     const scrollY = ownerDocument.defaultView.pageYOffset;

Specifications:

  • html2canvas version tested with:
  • Browser & version: Chrome 64.0.3282.186, Safari, Firefox
  • Operating system: OSX
@PeterRao
Copy link

PeterRao commented Jun 6, 2018

+1

1 similar comment
@Hellowor1d
Copy link

+1

@ngilmore14
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants