How to get height of pdf document load in iframe?

by cali_green , in category: HTML & CSS , 2 months ago

How to get height of pdf document load in iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 2 months ago

@cali_green 

To get the height of a PDF document loaded in an iframe, you can use JavaScript to access the iframe element and then get the height of the content inside the iframe. Here's a step-by-step guide on how to achieve this:

  1. Get the reference to the iframe element:
1
var iframe = document.getElementById('your-iframe-id');


  1. Access the content document inside the iframe:
1
var doc = iframe.contentDocument || iframe.contentWindow.document;


  1. Get the height of the content inside the iframe:
1
var height = doc.body.scrollHeight;


  1. You can now use the height variable to get the height of the PDF document loaded in the iframe.


Here is an example code snippet that you can use:

1
2
3
4
var iframe = document.getElementById('your-iframe-id');
var doc = iframe.contentDocument || iframe.contentWindow.document;
var height = doc.body.scrollHeight;
console.log('Height of the PDF document: ' + height);


Make sure to replace 'your-iframe-id' with the actual ID of the iframe element in your HTML document.


By following these steps, you should be able to get the height of a PDF document loaded in an iframe using JavaScript.