Untuk Mengunduh PDF dari tautan HTML menggunakan PHP dengan bantuan fungsi header[] di php. Fungsi header[] digunakan untuk mengirim header HTTP mentah. Terkadang ingin pengguna diminta untuk menyimpan data seperti PDF yang dihasilkan
Sintaksis
header["Content-Type: application/octet-stream"];
header['Content-Disposition: attachment; filename="downloaded.pdf"'];
header["Content-Length: " . filesize["download.pdf"]];
readfile['original.pdf'];
.
Catatan. Ingat bahwa HTTP header[] harus dipanggil sebelum keluaran aktual dikirim, baik dengan tag HTML normal, baris kosong dalam file atau dari PHP
Contoh 1. Simpan di bawah file HTML sebagai htmllinkpdf. html dan simpan file PHP sebagai unduhan pdf. php
- Contoh di bawah ini untuk mengilustrasikan konsep mengunduh file PDF menggunakan tautan HTML
- Di sini mengunduh file tampaknya dalam format PDF tetapi tanpa konten apa pun yang menunjukkan kesalahan saat membuka di aplikasi apa pun
- kode HTML
header['Content-Disposition: attachment; filename="downloaded.pdf"'];
2header['Content-Disposition: attachment; filename="downloaded.pdf"'];
_2header["Content-Length: " . filesize["download.pdf"]];
9________2______9
header["Content-Length: " . filesize["download.pdf"]];
9header['Content-Disposition: attachment; filename="downloaded.pdf"'];
80
>
header["Content-Length: " . filesize["download.pdf"]];
94
>
5>
6>
7>
_9readfile['original.pdf'];
34
header['Content-Disposition: attachment; filename="downloaded.pdf"'];
81>
6>
7header['Content-Disposition: attachment; filename="downloaded.pdf"'];
86header['Content-Disposition: attachment; filename="downloaded.pdf"'];
87>
0________1______89header['Content-Disposition: attachment; filename="downloaded.pdf"'];
91header['Content-Disposition: attachment; filename="downloaded.pdf"'];
94header['Content-Disposition: attachment; filename="downloaded.pdf"'];
95header['Content-Disposition: attachment; filename="downloaded.pdf"'];
97header['Content-Disposition: attachment; filename="downloaded.pdf"'];
98header['Content-Disposition: attachment; filename="downloaded.pdf"'];
990
header["Content-Length: " . filesize["download.pdf"]];
02header["Content-Length: " . filesize["download.pdf"]];
04header["Content-Length: " . filesize["download.pdf"]];
05header["Content-Length: " . filesize["download.pdf"]];
06header["Content-Length: " . filesize["download.pdf"]];
08readfile['original.pdf'];
3header["Content-Length: " . filesize["download.pdf"]];
100