Is there any way to print out (to cups for example) html file with help PHP (linux server without X) ?
except shell_exec(), system() etc...
And when i mean print html file means print not source code, means print rendered result.
is it possible with help PHP ?
I know thath there is some possibilities like,
- run X server,
- run browser,
- exec terminal command which will ask browser open and print out html file
but without shell_exec is it possible ?
You'll need to render the file yourself into some sort of printable format such as a postscript or PDF file. There is a good PHP5 library to create PDFs from HTML called dompdf.
So generate the HTML output and store it into a string, you may want to use output buffering to do this. See the PHP manual.
Then you can use dompdf to render the HTML contents into a PDF file. Storing it into a temp directory probably makes sense if you don't need to keep the file.
Then, you can use the cups cli to print the file, see the cups documentation. e.g.
shell_exec('lp '.$filename);
Related
So I've been working a bit with HTML2FPDF, and I have converting HTML to PDF working all fine. However, I cannot get any PHP to work. Not even static PHP like
echo "hello world";
it is just completely ignored. I thought this would render as HTML and it then would be included in the PDF, but it doesn't.
I wanna be able to run some PHP functions and then for the result to come out as HTML like it normally would. Is there a way to do this?
I am thinking that one way of doing this is to load the PHP function first. Then when the function has run and the result shows as HTML, then convert the page. I have tried this for a few hours now, with all sorts of solutions, but I can just not get it to work in any way.
Is there a way to convert PHP results to PDF as well?
Sounds like you don't have it installed properly.
You have just to launch the following command on the root folder of your project:
composer require spipu/html2pdf
I m using the php function file_get_contents to parse a php file. But it seems that as soon as it is reading the php tags the file_get_contents is malfunctioning.
I checked the function with a normal text file, its functioning perfectly. But even if it finds php tags in a text file, the file is being half read. How can i find a way to get the full contents.
Is the file local? Or are you trying to get a remote file? How did you check that the content is not read? Echoing it to a browser might trick you because of the < char in <?php
Use htmlspecialchars or <pre> to view the whole text. Or just look at the source of the page.
I have been given an SWF to edit a link in the AS code.
The fact is the SWF uses some XML that is generated (actually retrieved) by PHP code from a database.
menuXML.load("/sub/page/dynamic.php?genre=" + genre);
so the point is we can use the same SWF 'mainfraim' and fill them with different animations/sources based on the link provided in dynamic.php?genre=###
Now, I've used Flash Decompiler Gold to extract all files in the SWF and can open it again in Adobe Flash to edit it. When done I enter CTRL+ENTER and there are immediately 4 compiler errors!! Errors:
1x < Unexpected 'if' encountered >
2x < Statement block must be terminated by '}' >
1x < Ecpected a field name after '.' operator. >
How can these errors be present, when the original SWF works perfectly??!
If I don't manage to solve this, I'll have to find out how to create an .php file the SWF tries to use which can select the proper resources (from a database I guess) to show them (using ?genre=###)
Thanks!
I'm not sure I understand your problem, but it seems like you need to change the url passed to the load method. It also seems like your swf is Actionscript 2.0.
Decompilers sort of work, but the fla file you can generate with a decompiler will seldom be useful to generate the same swf back. Sometimes the code is illegal, and almost always the graphics are screwed.
I once had to make some simple code changes (like changing a few urls and other simple stuff) to a swf, for which we had no sources (they were lost and there was no backup...).
I used flasm for this and it worked fine (also it wasn't as hard as I first supposed).
Flasm is not a decompiler, but a disassembler. It takes your swf, parses the actionscript bytecode and generates a text file with assembly-like code. You can edit that code and re-assemble the swf. It doesn't touch graphics and animations, so it was what I needed, and perhaps could work for you.
I've made a little test and it worked fine.
I started with this code in a fla:
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function(ok:Boolean):Void {
if(ok) {
_debug_txt.text = "ok";
_debug_txt.text = xml;
} else {
_debug_txt.text = "error";
}
};
xml.load("/sub/page/dynamic.php");
Next, I opened a cmd prompt (I'm on Windows), cd to the directory that contains the swf and run:
flasm -d test_flasm.swf > test_flasm.flm
This disassembles the swf into a text file test_flasm.flm. I have added flasm to my executables path, but you can just use the full path to the flasm.exe instead.
The relevant part of the .flm file looks like this:
setMember
push '/sub/page/dynamic.php', 1, 'xml'
getVariable
push 'load'
callMethod
Yours may vary, but if you look for the url, you'll find it. Next, changed that url to:
setMember
push 'test.xml', 1, 'xml'
getVariable
push 'load'
callMethod
Then, I reassembled the swf using:
flasm -a test_flasm.flm
And now, test_flasm.swf loads "test.xml" instead of "/sub/page/dynamic.php".
Hope this helps.
Is it possible to merge FDF data with a PDF file using PHP alone? Or is there no option but to use a 3rd party command line tool to achieve this?
If that is the case can someone point me in the direction of one?
I am currently outputting the FDF file to the browser in the hope that it will redirect the user to the filled in PDF but for some people that is not the case. The FDF contents is being output to the screen, even though I am using header('Content-type: application/vnd.fdf');
For future reference, it looks like there isn't a reliable way of doing it without a 3rd party app. Pdftk (http://www.accesspdf.com/pdftk/) ended up being my solution.
I first generated the FDF file as before, and then merged it into my PDF file using the following PHP code
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="Download.pdf"');
passthru("pdftk file.pdf fill_form data.fdf output - ");
exit;
It was much easier than I thought it would be. This instantly eliminates the need to hack around with headers and file extensions to ensure all browsers handle an FDF properly, as it simply makes the browser download the PDF file.
If you want the PDF output file to no longer be editable, use
passthru("pdftk file.pdf fill_form data.fdf output - flatten");
Apologies if this is basic stuff, just thought I'd put it all in one place so that people don't go through the headache that I endured.
N.B. If your PATH variable is not set, you will need to use the full path to pdftk i.e.
passthru("/usr/local/bin/pdftk file.pdf fill_form data.fdf output - flatten");
There is another way to day that not using passthru nor pdftk but just a script made in 2004 but still working well : forge_fdf
it helps you to build a fdf that you can incoporate straithly in your pdf, it means that you
Save this in a php file, let's say generatePdf.php
require_once('forge_fdf.php');
// leave this blank if we're associating the FDF w/ the PDF via URL
$pdf_form_url= "";
// default data; these two arrays must ultimately list all of the fields
// you desire to alter, even if you just want to set the 'hidden' flag;
//
//
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example
$fdf_data_strings['email']=mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'#gmail.com';
$fields_hidden= array();
$fields_readonly= array();
// set this to retry the previous state
$retry_b= false;
header( 'content-type: application/vnd.fdf' );
echo forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
Making a link to Pathtoyourpdf/nameofpdffile.pdf#FDF=generatePdf.php will open your PDF file in browser (alternatively there is a way to save it to disk i think i remember) and the field email will be filled with data from MYSQL : mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'#gmail.com'
It works with checkboxes, radio button,... It opens well in firefox, it has to be tested with other browsers.
More infos on PDF HACKS
by far the easiest way I have found to install pdftk on centos:
Install rpmforge repo - http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-5aabf02717d5b6b12d47edbc5811404998926a1b
then
yum install pdftk
and thats it!
http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/
This works , the classes download are linked to from the web site too,
It requires no passthru/exec command and no additional extensions.
Edited to say, this doesn't work with newer pdf versions 1.5+, reverted to PDFTK, fiddly but works with all pdfs using the 'exec' command.
I have an RTF file that I want to display inside a web page after tags have been replaced with user input.
I would like to be able to display the RTF file without having to convert it to something before displaying it.
Every time I try it now it gives me the popup open/save box even though I am telling it to display it inline with:
header("Content-type: application/msword");
header("Content-Disposition: inline; filename=mark.rtf");
header("Content-length: " . strlen($output));
echo $output;
Most browsers won't reliably display RTF content. It IS possible to parse the RTF into HTML, and display the HTML content on your web page however.
You need some kind of program to parse RTF and convert it to HTML. I'm assuming it has to be free. I do not know of any reliable free RTF parsing or RTF to HTML libraries in PHP.
I recommend you use a command-line conversion program like RTF2HTML: http://sageshome.net/?w=downloads/soft/RTF2HTML.html
You would need to download and install this program on your webserver, allow the user to upload the file to a temp directory, and then call the command line application from PHP with shell_exec():
$html_output_path = '/path/for/processing/files/'
$html_output_filename = $username . $timestamp;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])
{
shell_exec('rtf2html ' .
escapeshellarg($_FILES['userfile']['tmp_name']) . " " .
$html_output_path . $html_output_filename);
}
$html_to_display = file_get_contents($html_output_path .
$html_output_filename);
Then, parse the results as HTML and display them. Not a bad strategy. Note that you will probably need to remove the head, body and possibly other tags if you're going to display the content inside another web page.
You might want to check out https://github.com/tbluemel/rtf.js for client-side RTF rendering. It's still in its early stages but it renders even embedded graphics. Support for rendering embedded WMF artwork is still very very limited, though, and requires browser support for the tag.
You needed an RTF to HTML converter written in PHP. I think this page contains your solution:
http://www.websofia.com/2014/05/a-working-rtf-to-html-converter-in-php/
First: you've got your content-type wrong. for RTF it's text/rtf
Second: you'll only be able to display in-line this type of content, which can be rendered by the web browser. RTF is not one of these. So you won't be able to display it in-line without converting it, or without some plug-in for the browser. Of course conversion might be on-the-fly.
Web pages can only contain HTML. You would need a browser plugin like flash to display other file types. See Scribd for example.
This isn't exactly an answer to your question, but one thing you may want to do is remove the "filename=mark.rtf" from the header. I've had browsers treat something as a download if I include "filename" in the header, even if the "Content-Disposition" is "inline".
You can't just output a file from within PHP code. You need to extract the data from it, then print the contents inline.
The php function 'file_get_contents' may do what you need. The functions manual is here: http://us2.php.net/filegetcontents
A sample usage is here:
$contents = file_get_contents('yourfile.rtf');