I am trying Ajax download PDF. Following is my Ajax code:
$.ajax({
// `url`
url: "/pdf/download",
type: "GET",
contentType: "application/octet-stream; charset=utf-8",
// `custom header`
headers: {
"OAuth-Token": api.getOAuthToken()
},
beforeSend: function (jqxhr) {
console.log(this.headers);
alert("custom headers" + JSON.stringify(this.headers));
},
success: function (data) {
// `file download`
$("a")
.attr({
"href": data.file,
"download": "file.pdf"
})
.html($("a").attr("download"))
.get(0).click();
//console.log(JSON.parse(JSON.stringify(data)));
},
error: function (jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown)
}
});
Following is my PHP Header Script:
$api->setHeader('Content-Transfer-Encoding', 'binary');
$api->setHeader('Cache-Control', 'public, must-revalidate, max-age=0');
$api->setHeader('Pragma', 'public');
$api->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT');
$api->setHeader('Last-Modified', $now->format(TimeDate::RFC2616_FORMAT));
$api->setHeader('Content-Type', 'application/pdf');
$api->setHeader(
'Content-Disposition',
'attachment; filename="' . getPdfName() . '.pdf"'
);
The above Ajac call with OAuth-Tocken is working fine but i receive the output like the following and the PDF is Blank and cannout open sometimes.
%PDF-1.4 %âãÏÓ 3 0 obj <
The above return data is justa sample of return data. Can some one tell me how can create a valide PDF from the data?
NOTE: I am trying to do this method because i couldnt able to achive sugarcrm fileDownload error after upgrade download in SugarCRM after latest update.