I have created this report using BIRT and phpjavabridge
<?php
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=downloaded.pdf");
require_once("http://127.0.0.1:8080/JavaBridge/java/Java.inc");
header("Content-type: text/html");
// the report file to render
$myReport = "test.rptdesign";
// load resources, .rpt files and images from the current working dir
$here = getcwd();
$ctx = java_context()->getServletContext();
$birtReportEngine = java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());
// Create a HTML render context
try{
// Load the report design
$design = $birtReportEngine->openReportDesign("${here}/${myReport}");
$task = $birtReportEngine->createRunAndRenderTask( $design );
$task->setParameterValue("sample", new java("java.lang.String", "Hello world!"));
// Add HTML render options
$options = new java("org.eclipse.birt.report.engine.api.PDFRenderOption");
$options->setOutputFormat($options->OUTPUT_FORMAT_PDF);
// Create the output
$out = new java("java.io.ByteArrayOutputStream");
$options->setOutputStream($out);
$task->setRenderOption($options);
$task->run ();
$task->close();
} catch (JavaException $e) {
echo $e; //"Error Calling BIRT";
}
// Return the generated output to the client
echo java_values($out->toByteArray());
?>
The report viewed perfectly inside Internet Explorer 8.0 as it triggered the Adobe Acrobat plugin. The problem is when I opened the report inside Mozilla Firefox 3.5.4 and Google Chrome 4.0.233 it showed me the binary string content of the pdf file, instead of triggering the Adobe Acrobat plugin.
I have checked this by putting a pdf file in the htdoc folder and call it from Firefox and Chrome, it worked just fine. But why the header wont work for the report?
*Also why the header only work for IE 8.0? I need the report to be viewed in all major browser
Content-type should be "application/pdf"
(or "application/x-pdf", for more recent pdf formats)
I think that if you change
header("Content-type: text/html");
to
header("Content-type: application/pdf");
in the code above, the browsers mentioned should start rendering the pdf documents properly (provided they are so configured), and IE will continue working ok (IE has some Automatic MIME Type Detection (based on the first few hundred bytes of content), which is a mixed blessing...)
Related
I need to upload a pdf file through backend API using JMeter. So for that, I passed a multipart API request. To upload the file I am using BeanShell Preprocessor.
FileInputStream in = new FileInputStream("C:\\Users\\XYZ\\Downloads\\PT_003.pdf");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int i; (i = in.read(buffer)) != -1; ) {
bos.write(buffer, 0, i);
}
in.close();
byte[] binarydata = bos.toByteArray();
bos.close();
vars.put("binarydata", new String(binarydata));
Multipart Request Body :
--AaC07x
Content-Type: application/json; charset=utf-8
{
"token":"a6b8J000000055JQPU",
"flow":"Development"
}
--AaC07x
content-disposition: form-data; name="File"; filename="PT_003.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: bytecode
${binarydata}
--AaC07x--
Header Manager:-
Content-Type multipart/related;boundary="AaC07x"
File uploaded but content in that pdf file is missing means when I tried to open the uploaded pdf file it's blank/corrupted.
So can you please anyone help me to fix that issue??
It looks like your byte array to string conversion is the problem. Moreover as per JMeter 5.5 you cannot simply send a byte array using HTTP Request sampler as given you put everything into "Body Data" tab it will be treated as a String
Since JMeter 3.1 you should avoid using Beanshell, the recommended scripting language is Groovy
If you have problems coming up with a proper request definition you can just record it using HTTP(S) Test Script Recorder, just make sure that your PT_003.pdf is in JMeter's "bin" folder during both recording and replay
I am using Jsreport via API.
From the browser, a ajax call is made to jsreport server. The server answers with POST respond with data and a Header tag Permanent-Link which has the file locaton.
Copy paste it to the browser allow me to view the pdf file.
The problem it that I want to view it automatically in success handler of ajax call, but xhr.getRespondHeader() does not allow any other header than Content-Type. The respond header even have "Access-Control-Allow-Origin: *" already.
How can I get the pdf out for user?
You can use official jsreport browser client - http://jsreport.net/learn/browser-client
If it is loaded in the page, opening a report is as simple as this
jsreport.serverUrl = 'http://localhost:3000';
var request = {
template: {
content: 'foo', engine: 'none', recipe: 'phantom-pdf'
}
};
//display report in the new tab
jsreport.render('_blank', request);
You can also check its source code if you are curious how it is handling AJAX
https://github.com/jsreport/jsreport-browser-client-dist
I have a script that works in PhantomJS but I'm trying to switch to SlimerJS. I keep getting an error when trying to open a local file:
var webPage = require('webpage');
var system = require('system');
var page = webPage.create();
page.viewportSize = { width: 2048, height: 1536 };
console.log('Processing',system.args[1]);
page.open(
'simple.html',
function start(status) {
setTimeout(function(){
page.render(system.args[2], {format: 'png'});
phantom.exit();
},1000);
}
);
simple.html is a file located in the same directory as the script. The resulting PNG says "Address Not Found", "simple.html could not be found. Please check the name and try again."
I've also tried:
full OS path, eg /User/blah/blah/simple.html
file URI file:///Users/blah/blah/simple.html
These yield a similar result.
I'd rather not have the script publicly available for a variety of reasons. Is it possible to launch a local file with SlimerJS?
I don't think its possible. Reading the docs it specifies a url.
I got around this by running a http server
python -m SimpleHTTPServer
Then accessing it through localhost.
page.open('http://localhost:8000/simple.html',...)
A file URI does work. Something like file:///Users/name/project/file.html.
I want to use uploadify with Symfony 1.4, but so far I couldn't.
Uploadify loads correctly, I choose my files, it says that the files were successfully uploaded, but the are nowhere.
(I'm doing this on localhost)
Is there anybody who met this problem before?
Thanks, Tom
$file = $request->getParameter('file');
$filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/'.$filename);
in my project session stored in cookies so I found solution by create extra session storage class
class MySessionStorage extends sfSessionStorage
{
public function initialize($options = null)
{
$request = sfContext::getInstance()->getRequest();
// work-around for uploadify
if ($request->getParameter('uploadify') == "onUpload")
{
$sessionName = $options["session_name"];
if($value = $request->getParameter($sessionName))
{
session_name($sessionName);
session_id($value);
}
}
parent::initialize($options);
}
}
then changed factories.yml to
all:
storage:
class: MySessionStorage
and then "uploader" param will like this
uploader : '<?php echo url_for("attachments/upload?uploadify=onUpload&" . session_name() . "=" . session_id(), true)?>',
I can only guess that it's because you're trying to upload while logged into a system, but flash does not inherit session data from the browser, this means you will always be denied permission to whatever function you are trying to access since symfony thinks you're not logged in.
So you need to manually set variables in order for flash to use the same login session as the browser:
jQuery Code (needs to be in a php file, will not work in a js file):
$('#file_upload').uploadify({
.... config here
'scriptData': { '<?php echo session_name() ?>': '<?php echo session_id() ?>' }
});
I'm using Amazon S3 to put the mp3 file then allow our site visitor to download the mp3 from Amazon AWS. I use S3Fox to manage the file, everything seems working fine until recently we got many complaints from visitor that the mp3 was streamed via the browser instead of displaying browser save dialog.
I try for some mp3 and notice that for some mp3, the save dialog box is appear, and for some others they're streamed via browser. What can I do to force that the mp3 file will be downloaded instead of streamed via web browser....
Any help would be much appreciated.
Thanks
In order to do so you need to set the Content-Disposition header:
Content-disposition: attachment; filename=song.mp3
I don't think this is possible with S3Fox. You could use Bucket Explorer (not free) or write a script to upload the files.
Ok, it's been a long time since you ask this, but I had the same problem and I'd like to share my solution with the community, just in case someone else need to solve this thing. Of course, you can change Content-Type and Content-Disposition from the Amazon S3 Console, but the interesting thing is to do it programmatically.
The following code works fine for me:
require_once '../sdk-1.4.2.1/sdk.class.php';
// Instantiate the class
$s3 = new AmazonS3();
// Copy object over itself and modify headers
$response = $s3->copy_object(
array( // Source
'bucket' => 'your_bucket',
'filename' => 'Key/To/YourFile'
),
array( // Destination
'bucket' => 'your_bucket',
'filename' => 'Key/To/YourFile'
),
array( // Optional parameters
'headers' => array(
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment'
)
)
);
// Success?
var_dump($response->isOK());
Hope it can helps other struggling with the same trouble.
This ended up being my solution for force downloading files from AWS S3.
In safari the files were downloading as .html files until I stopped returning the readfile and just ran the function alone.
public function get_download($upload_id)
{
try {
$upload = Upload::find($upload_id);
if ($upload->deleted)
throw new Exception("This resource has been deleted.");
if ($upload->filename == '')
throw new Exception("No downloadable file found. Please email info#clouddueling.com for support.");
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename={$upload->uploaded_filename};");
readfile("https://s3.amazonaws.com/stackoverflow/uploads/" . $upload->filename);
exit;
} catch(Exception $e) {
return $e->getMessage();
}
}
In s3 management console window, right click and chose properties.
Click on metadata.
Click on add more metadata
Key: content-disposition
Value: attachment
Save. That's all.