my.openDocument

The my.openDocument API enables users to preview a PDF file within the mini program.

 

After clicking on the PDF file, the user can preview the file without leaving the mini program. After closing the preview, the user will still be at the current mini program page.

Sample code

.js

copy
// .js
my.downloadFile({
  // Sample url that is not real
      url: 'http://documentExample.com/claro.pdf',
      success({ apFilePath }) {
        my.hideLoading();
        my.openDocument({
          filePath: apFilePath,
          fileType: 'pdf',
          success: (res) => {
            console.log('open document success')
            }
          })
        }
      })

Parameters

Object type

Property

Type

Required

Description

filePathString

Yes

Obtain file path via my.downloadFile.

fileTypeString

Yes

File type.

successFunction

No

Callback function upon call success.

failFunction

No

Callback function upon call failure.

completeFunction

No

Callback function upon call completion (to be executed upon either call success or failure).

Legal values for fileType

Value

Description

pdfPDF format.

Error code

Error code

Description

Solution

4011File path is invalid or no permission to access incoming path.

Check the incoming file path.

4012

Preview files do not exist.

Make sure files that correspond to file path exist.

4013File format is not supported.

Currently preview of PDF files is supported.