PDA

View Full Version : How to call server code to download image



hcervantes
20 Mar 2012, 5:56 AM
I have PHP code that converts SVG into image and sends it back to the client. I've got it set up to call the server code with Ext.Ajax.request and form.submit but neither is working. I'm not sure if I'm missing headers or what. The PHP code is working because I can call it from another application fine. Can someone shed some light on a solution for this?
js code:


var xml = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" />';
Ext.create('Ext.form.Panel', {
title: 'Basic Form',
renderTo: Ext.getBody(),
bodyPadding: 5,
width: 350,
heigth: 100,

// Any configuration items here will be automatically passed along to
// the Ext.form.Basic instance when it gets created.

// The form will submit an AJAX request to this URL when submitted
url: 'http://10.81.20.84/Exporting/index.php',

items: [{
fieldLabel: 'Field',
name: 'theField'
}],

buttons: [{
text: 'Submit',
handler: function() {
// The getForm() method returns the Ext.form.Basic instance:
var form = this.up('form').getForm();
if (form.isValid()) {
// Submit the Ajax request and handle the response
form.submit({
params: {
filename: 'mySVG',
width: 800,
svg: xml,
type: 'image/svg+xml',
method: 'POST'
},
success: function(form, action) {
Ext.Msg.alert('Success', action.result.msg);
},
failure: function(form, action) {
Ext.Msg.alert('Failed', action.response.responseText);
}
});
}
}
}]
});


<?php
/**
* This file is part of the exporting module for Highcharts JS.
* www.highcharts.com/license
*
*
* Available POST variables:
*
* $tempName string The desired filename without extension
* $type string The MIME type for export.
* $width int The pixel width of the exported raster image. The height is calculated.
* $svg string The SVG source code to convert.
*/


// Options
define ('BATIK_PATH', 'Java\batik-rasterizer.jar');

///////////////////////////////////////////////////////////////////////////////
ini_set('magic_quotes_gpc', 'off');

//$type = $_POST['type'];
//$svg = (string) $_POST['svg'];
//$filename = (string) $_POST['filename'];
$type = $_REQUEST['type'];
$svg = (string) $_REQUEST['svg'];
$filename = (string) $_REQUEST['filename'];

// prepare variables
if (!$filename) $filename = 'chart';
if (get_magic_quotes_gpc()) {
$svg = stripslashes($svg);
}



$tempName = md5(rand());

// allow no other than predefined types
if ($type == 'image/png') {
$typeString = '-m image/png';
$ext = 'png';

} elseif ($type == 'image/jpeg') {
$typeString = '-m image/jpeg';
$ext = 'jpg';

} elseif ($type == 'application/pdf') {
$typeString = '-m application/pdf';
$ext = 'pdf';

} elseif ($type == 'image/svg+xml') {
$ext = 'svg';
}
$outfile = "temp/$tempName.$ext";

if (isset($typeString)) {

// size
//if ($_POST['width']) {
if ($_REQUEST['width']) {
$width = (int)$_REQUEST['width'];
if ($width) $width = "-w $width";
}

// generate the temporary file
if (!file_put_contents("temp/$tempName.svg", $svg)) {

die("Couldn't create temporary file. Check that the directory permissions for
the /temp directory are set to 777.");
}

// do the conversion
$output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width temp/$tempName.svg");
// catch error
if (!is_file($outfile) || filesize($outfile) < 10) {
echo "<pre>$output</pre>";
echo "Error while converting SVG. ";

if (strpos($output, 'SVGConverter.error.while.rasterizing.file') !== false) {
echo "SVG code for debugging: <hr/>";
echo htmlentities($svg);
}
}

// stream it
else {
header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
header("Content-Type: $type");
echo file_get_contents($outfile);
}

// delete it
unlink("temp/$tempName.svg");
unlink($outfile);

// SVG can be streamed directly back
} else if ($ext == 'svg') {
header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
header("Content-Type: $type");
echo $svg;

} else {
echo "Invalid type";
}
?>

mitchellsimoens
20 Mar 2012, 9:35 AM
Have you looked at the request and the response?