Skip to content

Commit 0892a5b

Browse files
committed
Error with Blob inputs fixed
1 parent 8935d92 commit 0892a5b

File tree

5 files changed

+101
-6
lines changed

5 files changed

+101
-6
lines changed

SampleApp/src/app/features/features.component.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,44 @@ <h3>On-Demand Actions</h3>
723723

724724
<mat-divider></mat-divider>
725725

726+
<h3>PDF Source Type Testing (Issue #283)</h3>
727+
<p class="section-description">
728+
Test loading PDFs from different source types: string URL, Blob, or Uint8Array.
729+
This verifies that Issue #283 (Blob loading TypeError) is fixed.
730+
</p>
731+
<div class="action-buttons">
732+
<button
733+
mat-raised-button
734+
color="accent"
735+
(click)="loadStringPdf()"
736+
[disabled]="currentSourceType === 'string'"
737+
>
738+
<mat-icon>link</mat-icon> Load from String URL
739+
</button>
740+
<button
741+
mat-raised-button
742+
color="accent"
743+
(click)="loadBlobPdf()"
744+
[disabled]="currentSourceType === 'blob'"
745+
>
746+
<mat-icon>code</mat-icon> Load from Blob
747+
</button>
748+
<button
749+
mat-raised-button
750+
color="accent"
751+
(click)="loadUint8ArrayPdf()"
752+
[disabled]="currentSourceType === 'uint8array'"
753+
>
754+
<mat-icon>data_array</mat-icon> Load from Uint8Array
755+
</button>
756+
</div>
757+
<div class="info-box">
758+
<mat-icon>info</mat-icon>
759+
<span>Current Source Type: <strong>{{ currentSourceType }}</strong></span>
760+
</div>
761+
762+
<mat-divider></mat-divider>
763+
726764
<h3>Error Handling Demo</h3>
727765
<div class="error-config">
728766
<mat-form-field class="full-width">

SampleApp/src/app/features/features.component.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class FeaturesComponent implements OnInit {
4040
interactiveErrorTpl!: TemplateRef<any>;
4141

4242
// Configuration properties - directly bound to the viewer
43-
public pdfSrc = "/assets/pdfjs/web/compressed.tracemonkey-pldi-09.pdf";
43+
public pdfSrc: string | Blob | Uint8Array = "/assets/pdfjs/web/compressed.tracemonkey-pldi-09.pdf";
4444
public downloadFileName = "sample-document.pdf";
4545
public diagnosticLogs = false;
4646

@@ -515,6 +515,43 @@ export class FeaturesComponent implements OnInit {
515515
}
516516
}
517517

518+
// Test Blob and Uint8Array loading (Issue #283)
519+
public currentSourceType: 'string' | 'blob' | 'uint8array' = 'string';
520+
521+
public async loadBlobPdf() {
522+
try {
523+
console.log("🧪 Testing Blob pdfSrc loading...");
524+
const response = await fetch('/assets/pdfjs/web/compressed.tracemonkey-pldi-09.pdf');
525+
const blob = await response.blob();
526+
this.pdfSrc = blob;
527+
this.currentSourceType = 'blob';
528+
console.log("✅ Blob pdfSrc set successfully:", blob);
529+
} catch (error) {
530+
console.error("❌ Blob loading failed:", error);
531+
}
532+
}
533+
534+
public async loadUint8ArrayPdf() {
535+
try {
536+
console.log("🧪 Testing Uint8Array pdfSrc loading...");
537+
const response = await fetch('/assets/pdfjs/web/compressed.tracemonkey-pldi-09.pdf');
538+
const arrayBuffer = await response.arrayBuffer();
539+
const uint8Array = new Uint8Array(arrayBuffer);
540+
this.pdfSrc = uint8Array;
541+
this.currentSourceType = 'uint8array';
542+
console.log("✅ Uint8Array pdfSrc set successfully:", uint8Array);
543+
} catch (error) {
544+
console.error("❌ Uint8Array loading failed:", error);
545+
}
546+
}
547+
548+
public loadStringPdf() {
549+
console.log("🧪 Testing string pdfSrc loading...");
550+
this.pdfSrc = "/assets/pdfjs/web/compressed.tracemonkey-pldi-09.pdf";
551+
this.currentSourceType = 'string';
552+
console.log("✅ String pdfSrc set successfully");
553+
}
554+
518555
// Convenience setter demonstrations
519556
public useTraditionalApproach = true;
520557

docs-website/docs/getting-started.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export class AppModule { }
4848
</ng2-pdfjs-viewer>
4949
```
5050

51+
The `pdfSrc` property accepts URLs (strings), Blob objects, or Uint8Array byte arrays.
52+
5153
### Step 4: Add a PDF File
5254

5355
Place a PDF file in your `src/assets/` folder (e.g., `sample.pdf`).

docs-website/docs/intro.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export class AppModule {}
5555
</ng2-pdfjs-viewer>
5656
```
5757

58+
The `pdfSrc` property accepts URLs (strings), Blob objects, or Uint8Array byte arrays.
59+
5860
That's it! Your PDF viewer is ready to use.
5961

6062
## Why the Rewrite?

lib/src/ng2-pdfjs-viewer.component.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,20 +732,36 @@ export class PdfJsViewerComponent
732732
// Set up PostMessage listener
733733
this.setupMessageListener();
734734

735-
// Load PDF for embedded views.
736-
if (!this.externalWindow) {
737-
this.loadPdf();
738-
}
735+
// Note: PDF loading moved to ngAfterViewInit() to ensure iframe is ready
736+
// This prevents "Cannot read properties of null (reading 'location')" error
737+
// when pdfSrc is a Blob (Issue #283)
739738

740739
// Bind events.
741740
this.bindToPdfJsEventBus();
742741
}
743742

744743
ngAfterViewInit(): void {
745-
// Minimal initialization - rely on event-driven readiness notifications
744+
// Load PDF after view is initialized - trust Angular's lifecycle guarantee
745+
// that iframe.nativeElement.contentWindow is now available
746+
if (!this.externalWindow) {
747+
this.loadPdf();
748+
}
746749
}
747750

748751
ngOnChanges(changes: SimpleChanges): void {
752+
// Handle pdfSrc changes - reload the PDF (Issue #283)
753+
// Trust Angular's change detection event to trigger reload when needed
754+
if (changes['pdfSrc'] && !changes['pdfSrc'].firstChange) {
755+
// pdfSrc changed after initialization - reload the PDF
756+
if (!this.externalWindow) {
757+
// Reset configuration queuing since iframe will reload fresh
758+
this.initialConfigQueued = false;
759+
this.isPostMessageReady = false;
760+
this.loadPdf();
761+
}
762+
return; // pdfSrc change requires full reload, skip other change processing
763+
}
764+
749765
if (this.PDFViewerApplication) {
750766
// Only apply changes if PostMessage API is ready and viewer is initialized
751767
if (this.isPostMessageReady && this.PDFViewerApplication.initialized) {

0 commit comments

Comments
 (0)