How can I help you?
Defer document loading until PDFium resources are ready
28 Feb 20262 minutes to read
In Standalone mode, the Syncfusion® ASP.NET Core PDF Viewer downloads the required PDFium runtime assets (scripts and WebAssembly) from the location specified in the resourceUrl property. Attempting to load a document before these assets are fully initialized can lead to errors. Use the resourcesLoaded event to ensure the document is only loaded once the viewer’s runtime is fully prepared.
When does resourcesLoaded trigger?
The resourcesLoaded event fires after the viewer has successfully downloaded and initialized all internal PDFium dependencies. This event indicates that it is safe to invoke the load() method via the viewer instance.
Follow these steps to synchronize document loading with asset readiness:
- Specify a valid
resourceUrl(using a CDN or a locally hosted path). - Assign a handler to the
resourcesLoadedevent. - Programmatically call the
load()method inside the handler.
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<ejs-pdfviewer
id="pdfViewer"
style="height:600px; display:block"
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib"
resourcesLoaded="onResourcesLoaded">
</ejs-pdfviewer>
</div>
<script>
// Choose one of the following load sources:
var documentUrl = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf';
var base64 = ''; // put Base64 if you want to load from base64
// Called automatically when the PDF Viewer’s resources (worker/lib) are ready
function onResourcesLoaded(args) {
var viewer = document.getElementById('pdfViewer').ej2_instances[0];
// 1) Load by URL (most common)
viewer.load(documentUrl, '');
// 2) Load by Base64 (uncomment if needed)
// if (base64) {
// viewer.load(base64, '');
// }
}
</script>Best practices
-
Path Verification: Ensure the
resourceUrlis accurate and accessible. If the network blocks the asset location or the path is incorrect, theresourcesLoadedevent will not fire. -
One-Time Lifecycle: The
resourcesLoadedevent typically fires once during the component’s lifecycle initialization. Subsequent document loads (e.g., in response to user selection) do not need to wait for this event if it has already occurred. -
Error Handling: For mission-critical viewers, monitor network failures for the
resourceUrlto provide appropriate fallback messages to the user.