0

I have a HTML page with Iframe content and i am trying to export it to PDF.The export to PDF is working but the JS query does not pull the IFrame content into the PDF file.

The

tag content alone is been published in pdf file.

Below listed the code for references.

<html>

<body>

  <div id="content">
    <p> Hello </p>
    <iframe src="https://app.powerbi.com/view?r=eyJrIjoiYjVjZTZkYjEtNWRiOC00ZTM2LWJiNGYtZTA0Y2I2YmFhN2Y4IiwidCI6ImJkMGQyNTQxLWEyZTAtNDE1Zi1hNTI2LTNkY2UxODFlMDM0OSIsImMiOjh9" style="height:750px;width:1550px;">
</iframe>
  </div>
  <div id="editor"></div>
  <button id="cmd">Generate PDF</button>

  <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>

  <Script>
    var doc = new jsPDF();
    var specialElementHandlers = {
      '#editor': function(element, renderer) {
        return true;
      }
    };

    $('#cmd').click(function() {
      doc.fromHTML($('#content').html(), 50, 50, {
        'width': 170,
        'height': 170,
        'elementHandlers': specialElementHandlers
      });
      doc.save('sample-file.pdf');
    });

    function getFrameContents() {
      var iFrame = document.getElementById('#content');
      var iFrameBody;
      if (iFrame.contentDocument) { // FF
        iFrameBody = iFrame.contentDocument.getElementsByTagName('#content')[1];
      } else if (iFrame.contentWindow) { // IE
        iFrameBody = iFrame.contentWindow.document.getElementsByTagName('#content')[1];
      }
      //alert(iFrameBody.innerHTML);
      return iFrameBody.innerHTML
    }
  </Script>
</body>

</html>
Andrzej Ziółek
  • 2,241
  • 1
  • 13
  • 21
varun p
  • 1
  • 1
  • 3

1 Answers1

0

var iFrame = document.getElementById('#content')

You are not asking for the iframe, but the div#content. Try with: var iFrame = $('#content > iframe');

getElementById does not use #.

getElementsByTagName should ask for a tag, not a jQuery id.

Try using jQuery instead of plain JavaScript when possible for cleaner look.

  • I tried as you have mentioned but not able to get the Iframe content. Can you share me the modified code if possible. – varun p Jul 13 '18 at 09:58
  • If you are trying to get a cross site content, it's not possible. Else try with $('#content iframe').contents().find('body').html() https://stackoverflow.com/questions/6170925/get-dom-content-of-cross-domain-iframe –  Jul 13 '18 at 12:52