Answer the question
In order to leave comments, you need to log in
AJAX request not downloading PDF file (FPDF)?
On the part of the User, data is transmitted to the server using AJAX, a PDF is generated on the server with the transferred values, the request is processed, but the PDF file itself is not downloaded, although when you go to it by entering the URL of the server file, it is downloaded, but as you understand without transferred files( I use the built-in class in PHP, "FPDF"
let obj = {
name: "Roma",
secondName: "Ishutin",
year: "21"
};
const url = "pdf.php";
let button = document.querySelector("#button");
button.addEventListener("click", () => {
fetch(url, {
method: "POST",
body: JSON.stringify(obj),
headers: {
"Content-type": "application/json"
}
});
});
$serverObj = $_POST;
$serverObj = json_decode(file_get_contents("php://input"), true);
$name = $serverObj["name"];
$secondName = $serverObj["secondName"];
$year = $serverObj["year"];
require("fpdf-utf8/tfpdf.php");
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->SetFont('DejaVu','',14);
////////////////////////////////////////////////////////////////////
$pdf->SetAuthor("Tonator");
$pdf->SetTitle("Price");
$pdf->SetDisplayMode('real','default');
$pdf->Image('logo.png',69,11,71,12,'png', 'http://www.fpdf.org/');
$pdf->SetXY(10,30);
$pdf->SetFontSize(14);
$pdf->Write(14,'Имя: ');
$pdf->Write(14,$name);
$pdf->SetXY(10,40);
$pdf->SetFontSize(14);
$pdf->Write(14,'Фамилия: ');
$pdf->Write(14,$secondName);
$pdf->SetXY(10,50);
$pdf->SetFontSize(14);
$pdf->Write(14,'Годиков: ');
$pdf->Write(14,$year);
$result = $pdf->Output('example1.pdf','D');
Answer the question
In order to leave comments, you need to log in
If someone is interested, then I managed to implement it using BLOB. Like Mellorn said
let obj = {
name: "Roma",
secondName: "Ishutin",
year: "21"
};
const url = "pdf.php";
let button = document.querySelector("#button");
button.addEventListener("click", () => {
fetch(url, {
method: "POST",
body: JSON.stringify(obj),
headers: {
"Content-type": "application/json"
}
})
.then(response => response.blob())
.then(function (blob) {
let a = document.createElement('a');
a.className = "link";
a.innerHTML = "Скачать";
a.href = URL.createObjectURL(blob);
document.body.append(a);
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question