Angular iframe embed pdf adjust img width

本文探讨了在移动Safari上使用Angular显示PDF时遇到的问题,由于iOS 8以后,Safari会将PDF作为图片渲染在iframe中。解决方案是在PDF加载后调整iframe内图片的宽度。同时提供了在Angular 6中获取iframe内img元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problems displaying PDF in iFrame on Mobile Safari
https://stackoverflow.com/questions/15480804/problems-displaying-pdf-in-iframe-on-mobile-safari

As of iOS 8, mobile Safari renders the PDF as an image within an HTML document inside the frame. You can then adjust the width after the PDF loads:

<iframe id="theFrame" src="some.pdf" style="height:1000px; width:100%;"></iframe>
<script>
document.getElementById("theFrame").contentWindow.onload = function() {
    this.document.getElementsByTagName("img")[0].style.width="100%";
};
</script>

Get IFrame img in angular 6
https://stackoverflow.com/questions/53605978/get-iframe-img-in-angular-6

import { Component, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {

  @ViewChild('iframe') iframe: ElementRef;

  ngAfterViewInit() {
    const nativeEl =  this.iframe.nativeElement;
    if ( (nativeEl.contentDocument || nativeEl.contentWindow.document).readyState === 'complete' )        {
        nativeEl.onload = this.onIframeLoad.bind(this);
    } else {
      if (nativeEl.addEventListener) {
        nativeEl.addEventListener('load', this.onIframeLoad.bind(this), true);
      } else if (nativeEl.attachEvent) {
        nativeEl.attachEvent('onload', this.onIframeLoad.bind(this));
      }
    }
  }


  onIframeLoad() {
    const base64String = this.iframe.nativeElement.contentWindow.document.body.innerHTML;
    console.log(this.iframe.nativeElement.contentWindow.document.body.children[1].currentSrc);
  }
}

Note: iframe url

 constructor(public sanitizer: DomSanitizer) {}
  ngOnInit() {
    this.urlSafe = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值