Я использую JavaScript для получения информации из формы, заполненной пользователем, затем отправляю эти данные в PHP и генерирую PDF с FPDF.Проблема в том, что я хочу, чтобы браузер попросил пользователя сохранить PDF или просмотреть его в Интернете, но я не могу понять, как это сделать.PDF генерируется правильно, но только когда я сохраняю его по определенному пути, указанному мной.
Вопрос в том, как вы, ребята, отправляете данные из JavaScript в PHP для создания PDF-файла, а затем браузер просит пользователя открыть или загрузить его, или как создать функцию, с помощью которой пользователь может извлечь этот PDF-файл.
JavaScript:
function senddata() {//this activates when i push a button not a submit
var peticion = new XMLHttpRequest();
peticion.open('POST', 'generatepdf.php');
var nueva2 = {};
var key;
for (i = 0; i < 6; i++) {
key = document.forms[0].elements[i].id;
nueva2[key] = document.forms[0].elements[i].value;
}//here i take my data from the form and make an object
var json = JSON.stringify(nueva2);//here i tranform my object to json string so i can send it to my php
var parametros = "json_string=" + json;//here I do this so later I can easily transform the $_POST to an array in the form json_decode($_POST["json_string"],true);
peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
peticion.send(parametros);//it sends ok
}
PHP с классом FPDF и вещами
<?php
require('fpdf/fpdf.php');
require('functions.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
$datos=json_decode($_POST["json_string"],true); //here i have my data in an array format so i can use the parameters to fill my pdf fields
//...lots of pdf things here...//
$pdf->Output('F',"pdfs/Cotizacion ".$datos["nombres"]." ".$datos["apellidos"].".pdf");//this works but never ask the user
//$pdf->Output('D',"pdfs/Cotizacion ".$datos["nombres"]." ".$datos["apellidos"].".pdf");//this should force a dowload or send to the browser but doesnt work
//$pdf->Output();//this should send to the browser but doesnt work
}