Skip to content

For developers: How Obsidian loads PDF files

Ryota Ushio edited this page Feb 16, 2024 · 3 revisions

Workspace-level

First, I will explain what happens when Obsidian opens a file in general, not limited to PDFs.

(What's a workspace?)

When Obsidian tries to open a file, first a WorkspaceLeaf's openFile method is called. In turn, it triggers WorkspaceLeaf.prototype.setViewState.

In setViewState, Obsidian identifies which view type to use to open the file. For example, PDFView is selected for PDF files.

Note

PDFView is not included in the public API. See typings.d.ts to see what it looks like.

Then, Obsidian instantiates a view object of the type and calls its open method (undocumented), which in turn calls the load method. At this point, the file is not opened in that view. After that, the view's setState method is triggered, in which loadFile (undocumented) is executed in the case of FileViews.

PDF-specific processes

Next, I will describe the processes specific to PDF files.

class PDFView extends EditableFileView

Registers a vault.on("modify", ...) event handler, which calls the onLoadFile method of the associated PDFViewerComponent.

Calls the loadFile method of the associated PDFViewerComponent.

class PDFViewerComponent extends Component

Creates a PDFViewerChild object and calls its load method.

loadFile

Calls the loadFile method of the child: PDFViewerChild.

class PDFViewerChild

Wraps PDF.js viewer and bridges between PDF.js and Obsidian.