Buka PDF dari JS

Ini tulisan supaya gak cape nyari-nyari lagi di Google. 😐

Kalo mau buka PDF dari JavaScript di browser, bisa pake cara ini. Contoh ini pakai axios sebagai http client.

async function openPdfWindow() 
{
    const pdfWindow = window.open()

    const url = 'dummy.pdf'

    const response = await axios.get(url, {
        responseType: 'blob',
    })

    const file = new Blob([response.data], {type: 'application/pdf'})

    const fileURL = URL.createObjectURL(file)

    pdfWindow.location = fileURL
}

Alternatif lain bisa pake base64.

Iklan

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s