> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/get-started/guides/features/basics/open.md).

# Open a PDF using JavaScript

Learn how to open a PDF document from a memory buffer with full source code examples. Explore the PDFDoc constructor and best practices for handling PDF documents efficiently. The Apryse Web SDK strea

{% hint style="warning" %}
Make sure you have [Full API enabled in WebViewer.](/web/what-is-webviewer/full-api.md)
{% endhint %}

To open a PDF document.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
// open document from the filesystem or URL
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);

  // optionally read a PDF document from a stream
  const file = await PDFNet.Filter.createURLFilter(filename);
  const doc_stream = await PDFNet.PDFDoc.createFromFilter(file);

  // or pass-in a memory buffer
  const file_sz = await file.size();
  const file_reader = await PDFNet.FilterReader.create(file);
  const mem = await file_reader.read(file_sz);
  const doc_mem = await PDFNet.PDFDoc.createFromBuffer(mem);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Read & write a PDF file from/to memory buffer](/web/get-started/samples.md#pdfdocmemory) Full source code which illustrates how to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDF documents that don't need to be saved/read from a disk.

## About opening a document

The PDFDoc constructor creates a PDF document from scratch:

{% hint style="warning" %}
**PDFDoc.Close()**

When you are finished with a PDFDoc object, the PDFDoc.Close() method should be called to clean up memory, file handles, and resources.
{% endhint %}

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const doc = await PDFNet.PDFDoc.create();
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}
{% endtabs %}

A newly-created document does not yet contain any pages. See the [accessing pages](/web/get-started/guides/features/basics/pages.md) section for details on creating new pages and working with existing pages.

Using Apryse SDK, you can open a document from a serialized file, from a memory buffer, or from a Filter stream.

To open an existing PDF document from a file, specify its file path in the PDFDoc constructor:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}
{% endtabs %}

Here's how to open an existing PDF document from a memory buffer:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const file = await PDFNet.Filter.createURLFilter(filename);
  const file_sz = await file.size();
  const file_reader = await PDFNet.FilterReader.create(file);
  const mem = await file_reader.read(file_sz);
  const doc_mem = await PDFNet.PDFDoc.createFromBuffer(mem);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}
{% endtabs %}

It's also easy to open a PDF document from a [MemoryFilter or a custom Filter ](/web/low-level-pdf-api/filters.md).

After creating a PDFDoc object, it's good practice to call `InitSecurityHandler()` on it. If the document is encrypted, calling the method will decrypt it. If the document is not encrypted, calling the method is harmless.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);
  if (!doc.initSecurityHandler()) {
    console.log("Document authentication error...");
    return;
  }
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/get-started/guides/features/basics/open.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
