File tree Expand file tree Collapse file tree
src/components/BootstrapBlazor.PdfViewer Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+ // Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+ using Microsoft . AspNetCore . Components ;
6+
7+ namespace BootstrapBlazor . Components ;
8+
9+ /// <summary>
10+ /// PdfViewer component for displaying PDF files in a Blazor application.
11+ /// </summary>
12+ public partial class PdfViewer
13+ {
14+ /// <summary>
15+ /// Gets or sets the url for the PDF file to be displayed.
16+ /// </summary>
17+ [ Parameter ]
18+ public string ? Url { get ; set ; }
19+
20+ /// <summary>
21+ /// Gets or sets the viewer height. Default is null.
22+ /// </summary>
23+ [ Parameter ]
24+ public string ? Height { get ; set ; }
25+
26+ private string ? ClassString => CssBuilder . Default ( "bb-pdf-viewer-container" )
27+ . AddClassFromAttributes ( AdditionalAttributes )
28+ . Build ( ) ;
29+
30+ private string ? StyleString => CssBuilder . Default ( )
31+ . AddClass ( $ "--bb-pdf-viewer-height: { Height } ;", ! string . IsNullOrEmpty ( Height ) )
32+ . Build ( ) ;
33+
34+ private string ? _url ;
35+
36+ /// <summary>
37+ /// <inheritdoc/>
38+ /// </summary>
39+ /// <param name="firstRender"></param>
40+ /// <returns></returns>
41+ protected override async Task OnAfterRenderAsync ( bool firstRender )
42+ {
43+ await base . OnAfterRenderAsync ( firstRender ) ;
44+
45+ if ( firstRender )
46+ {
47+ _url = Url ;
48+ }
49+
50+ if ( _url != Url )
51+ {
52+ _url = Url ;
53+ await InvokeVoidAsync ( "loadPdf" , Id , _url ) ;
54+ }
55+ }
56+
57+ /// <summary>
58+ /// <inheritdoc/>
59+ /// </summary>
60+ /// <returns></returns>
61+ protected override Task InvokeInitAsync ( ) => InvokeVoidAsync ( "init" , Id ) ;
62+ }
Original file line number Diff line number Diff line change 1+ import { addLink , addScript } from "../BootstrapBlazor/modules/utility.js"
2+ import Data from "../BootstrapBlazor/modules/data.js"
3+
4+ export async function init ( id ) {
5+ await addLink ( "./_content/BootstrapBlazor.PdfViewer/pdf-viewer.css" ) ;
6+
7+ const el = document . getElementById ( id ) ;
8+ const url = el . getAttribute ( 'data-bb-url' ) ;
9+ const pdfViewer = { el } ;
10+
11+ createFrame ( pdfViewer , url ) ;
12+ Data . set ( id , pdfViewer ) ;
13+
14+ console . log ( pdfViewer ) ;
15+ }
16+
17+ export function loadPdf ( id , url ) {
18+ const pdfViewer = Data . get ( id ) ;
19+ const { frame } = pdfViewer ;
20+ if ( frame ) {
21+ frame . src = url ;
22+ }
23+ else {
24+ createFrame ( pdfViewer , url ) ;
25+ }
26+ }
27+
28+ const createFrame = ( pdfViewer , url ) => {
29+ const { el } = pdfViewer ;
30+ if ( url ) {
31+ const frame = document . createElement ( 'iframe' ) ;
32+ frame . src = url ;
33+ frame . classList . add ( 'bb-pdf-viewer' ) ;
34+ el . appendChild ( frame ) ;
35+ pdfViewer . frame = frame ;
36+ }
37+ }
38+
39+ export function dispose ( id ) {
40+ }
Original file line number Diff line number Diff line change 1+ .bb-pdf-viewer-container {
2+ width : 100% ;
3+ height : var (--bb-pdf-viewer-height , 500px );
4+ }
5+
6+ .bb-pdf-viewer {
7+ width : 100% ;
8+ height : 100% ;
9+ }
You can’t perform that action at this time.
0 commit comments