ionic3项目实现在线预览PDF文件

 

文章转自:https://blog.csdn.net/Raytheon107/article/details/82690687

亲测有效。

第一步:安装 ng2-pdf-viewer

npm install ng2-pdf-viewer --save

第二步:新建页面用于展示pdf

注释:新建页面时需要删除.module.ts文件(去除懒加载)

ionic g page showpdf

第三步:app.module.ts

import {PdfViewerModule} from 'ng2-pdf-viewer';  
import { ShowpdfPage } from "../pages/showpdf/showpdf";
 
@NgModule({
    declarations: [
        ShowpdfPage
    ],
    imports: [      
        PdfViewerModule,
    ],
    entryComponents: [
        ShowpdfPage
    ],
})
--------------------- 
作者:Raytheon107 
来源:CSDN 
原文:https://blog.csdn.net/Raytheon107/article/details/82690687 
版权声明:本文为博主原创文章,转载请附上博文链接!

第四步: home页面

//html文件
 
<ion-row>
    <ion-col>
        <a style="text-decoration:underline"(click)="checkUrl(item.url)">附件</a>
    </ion-col>
</ion-row>
 
 
 
//ts文件
 
import {ShowpdfPage} from "../showpdf/showpdf";
@Component({
    selector: 'page-xxxx',
    templateUrl: 'xxxx.html'
})
export class XxxxPage {
 
    constructor(public modalCtrl: ModalController) {}
 
    checkUrl(url) {
        console.log(url)
        url= 'https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf' //测试数据
        let modal: Modal = this.modalCtrl.create(ShowpdfPage, {
            displayData: {
              pdfSource: {
                url: url
              }
            }
          });
         modal.present();
    }
}
--------------------- 
作者:Raytheon107 
来源:CSDN 
原文:https://blog.csdn.net/Raytheon107/article/details/82690687 
版权声明:本文为博主原创文章,转载请附上博文链接!

第五步:showpdf页面 


//Showpdf.html文件
 
<ion-content padding>
  <pdf-viewer [src]="displayData.pdfSource"
              [show-all]="true"
              [original-size]="false"
              [zoom]=1
              [render-text]="false"
              [autoresize]="true"
              style="display: block" >
  </pdf-viewer>
</ion-content>
 
 
//Showpdf.ts文件
 
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
 
@Component({
  selector: 'page-showpdf',
  templateUrl: 'showpdf.html',
})
export class ShowpdfPage {
 
  displayData: any = {};
 
  constructor(public navParams: NavParams) {
    this.displayData = this.navParams.get('displayData');
  }
 
  goBack(){
    this.navCtrl.pop();
  }
 
}
--------------------- 
作者:Raytheon107 
来源:CSDN 
原文:https://blog.csdn.net/Raytheon107/article/details/82690687 
版权声明:本文为博主原创文章,转载请附上博文链接!

到此完成。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ionic实现图片预览可以使用 Ionic 的 ion-slides 组件和 ngx-gallery 库。 1. 首先,在终端中运行以下命令安装 ngx-gallery 库: ``` npm install @kolkov/ngx-gallery --save ``` 2. 在 app.module.ts 中导入 NgxGalleryModule: ```typescript import { NgxGalleryModule } from '@kolkov/ngx-gallery'; @NgModule({ imports: [ // ... NgxGalleryModule ], // ... }) export class AppModule { } ``` 3. 在 HTML 中使用 ion-slides 组件,并在其中使用 ngx-gallery: ```html <ion-slides pager="true"> <ion-slide *ngFor="let image of images"> <ngx-gallery [options]="galleryOptions" [images]="[image]"></ngx-gallery> </ion-slide> </ion-slides> ``` 4. 在 TypeScript 文件中定义图片和 ngx-gallery 的选项: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'], }) export class HomePage { images = [ { small: 'https://picsum.photos/id/1018/200/300', medium: 'https://picsum.photos/id/1018/500/750', big: 'https://picsum.photos/id/1018/1200/1800', description: 'Image 1' }, { small: 'https://picsum.photos/id/1015/200/300', medium: 'https://picsum.photos/id/1015/500/750', big: 'https://picsum.photos/id/1015/1200/1800', description: 'Image 2' }, { small: 'https://picsum.photos/id/1019/200/300', medium: 'https://picsum.photos/id/1019/500/750', big: 'https://picsum.photos/id/1019/1200/1800', description: 'Image 3' } ]; galleryOptions = [ { width: '100%', height: 'auto', thumbnailsColumns: 4, imageAnimation: 'slide', preview: false } ]; constructor() {} } ``` 这样,就可以在 Ionic实现图片预览了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值