im creating a service that will transform html to pdf.
Request with parameter(HTML code to be transfered into PDF) ist send with ajax post.
On server side generating pdf file is no problem.
I was trying to send it back with JSON but i figured out that its not a good idea.
So now im sending it back with no changes - full pdf file as it was generated
...
$pdf->Output('out.pdf', 'I');
And here comes the tricky part.
What can i do with it on client side?
I was trying to show it or download it but i could not figured out how.
$.ajax({
type: 'POST',
url: service_url,
data: out,
success: function (data) {
// WHAT SHOULD I DO WITH "data" IF I WANT TO SHOW IT?
error: function () {
alert("Error");
}
});
Thanks
a wild guess would say that you need to create a PDFObject and then throw it into the Ajax.
HTML
<object id="pdf" data="myfile.pdf" type="application/pdf" width="100%" height="100%">
It appears you don't have a PDF plugin for this browser.
No biggie... you can click here to
download the PDF file.
</object>
and in the Ajax you would have to do something like :
$.ajax({
var pdf = $("#pdf");
type: 'POST',
url: service_url,
data: out,
success: function (data) {
pdf.show();
error: function () {
alert("Error");
}
});
Related
I can't get this to work. What I want to do: Write text to the file example.txt on my server.
var text = "Example;Example;Example";
function saveToTxt(text)
{
//Code which saves text to file
}
As far as I know, I have to use some kind of Ajax request like this:
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
Can someone give me an example on how to do this are any alternatives?
Thank you in advance!
This post talk about how to save and write file into server using js and php.
(Saving a text file on server using JavaScript).
This is another post where you can edit your file with php.(How to edit/update a txt file with php)
Hope this helps
I know that this question is already asked many times by different ways. But still i am not able to figure out the answer. I mean i did not find a proper descriptive answer.
I use mpdf library to generate PDF. I post some data of hidden fields to a PHP script by means of ajax.
Following are the code snippets.
//ajax_file
$("#button_id").click(function(e){
var table_clone=$("#table_id").clone();
var tableData=table_clone.prop('outerHTML');//html for pdf generation
dataString='page='+tableData;
$.ajax({
type: 'POST',
url: 'pdfgenerator.php',
data: dataString,
cache: false,
success: function(response)
{
//what to do here in order to display pdf
},
error: function(............){
.
.
}
});
});
PHP Script
//pdfgenerator.php
<?php
include('../mpdf/mpdf.php');
if(/*checking post items are set*/)
{
//retriving post items
$mpdf=new mPDF('c','A4-L');
$mpdf->WriteHTML($tableData);
$mpdf->output('xyz.pdf','I');
exit;
}
?>
Following are my constraints
-> I don't want to save file permanently on server (which is possible by means of 'F' option in output()).
-> I have to display it in browser from where it can be downloaded.
PHP script works correctly if called without ajax. Hence it returns correct data but i am unable to display it in pdf inside the browser.
While searching for answers i found that it is not possible by means of ajax.
so is there any way around by doing something in PHP or javascript. Please provide a descriptive answer.
Thanks,
You can use this code
$.ajax({
type: 'POST',
url: 'pdfgenerator.php',
data: dataString,
cache: false,
success: function(response)
{
var tag ='<object width="400" height="500" type="application/pdf" data="'+xyz.pdf+'" id="show_obj1" class="obj"></object>';
$(#pdfdiv).html(tag);
},
error: function(............){
.
.
}
});
});
Here #pdfdiv in $(#pdfdiv).html(tag); is the id of the div in which you want to show the pdf
I am trying to publish a network with Cytoscape web (or cytoscape.js if possible.) Since my data is quiet huge i prefer to export it from Cytoscape desktop and grab it with ajax in my html. Before the last version of Cytoscape (v.3.1.0) i was able to export the network in .xml format using this:
$.ajax({
type: "GET",
url: "data.xml",
dataType: "xml",
error: function(){
alert("Error loading file");
},
success: function(data){
data = (new XMLSerializer()).serializeToString(data);
});
vis.draw({ network: data });
and it was working just fine.
When I try to do the same thing with .json (either converted from .xml or exported as .cyjs from Cytoscape desktop) it doesn't work. I used a similar code for this:
$.ajax({
type: "GET",
url: "data.json",
dataType: "json",
error: function(){
alert("Error loading file");
},
success: function(data){
}
});
var netwdata = data.elements[0];
vis.draw({ network: netwdata });
Although I do not get a loading error, the network is not plotted now. I have no experience with .json so I am sure I am missing something. Any help or comment is welcome.
I think you didn't write serializer for json data. In simple words you didn't convert json object to string. Like you used for xml:
data = (new XMLSerializer()).serializeToString(data);
Hi I wanted to know if it is possible to get a secured XML file from an AJAX call.
I have the following code to get the xml file (which works):
function loadXMLFile() {
var filename = 'test.xml';
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: parseXML,
error: Fail
});
}
With secure I mean that people can not get the xml file through their browser.
There is no way to write JavaScript that will tell the user's browser how to get some data without also making that data available to the person who controls the browser.
I am trying to use BusinessObject RESTful API to download a generated (pdf or xls) document.
I am using the following request:
$.ajax({
url: server + "/biprws/raylight/v1/documents/" + documentId,
type: "GET",
contentType: "application/xml",
dataType: "text",
headers: {"X-SAP-LogonToken": token, "Accept": "application/pdf" },
success: function(mypdf) {
// some content to execute
}
});
I receive this data as a response:
%PDF-1.7
%äãÏÒ
5 0 obj
<</Length 6 0 R/Filter/FlateDecode>>
//data
//data
//data
%%EOF
I first assumed that it was a base64 content, so in order to allow the users to download the file, I added these lines in the success function:
var uriContent = "data:application/pdf; base64," + encodeURIComponent(mypdf);
var newWindow=window.open(uriContent, 'generated');
But all I have is an ERR_INVALID_URL, or a failure while opening the generated file when I remove "base64" from the uriContent.
Does anyone have any idea how I could use data response? I went here but it wasn't helful.
Thank you!
. bjorge .
Nothing much can be done from client-side i.e. JavaScript.
The server side coding has to be changed so that a url link is generated (pointing to the pdf file) and sent as part of the response. The user can download the pdf from the url link.
You cannot create file using javascript, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least.
To achieve your functionality, you can implement click event which target to your required file and it will ask about save that file to user.