Archive

Posts Tagged ‘directly’

Printing Directly From PHP

January 22nd, 2009 No comments

Some of you might be surprised that you can print directly to a printer of your choice through PHP. This uses a PECL extension but is only available on Windows. It is possible to print using UNIX systems, but you have to call the ps program using the system function.

To install the printer functions on Windows you need to download the PECL library for Windows, found at pecl4win.php.net. Unzip this file and move the file php_printer.dll to your extensions directory in your PHP dicrectory (usually called ext).

Next, open up your php.ini file and add the following line at the end of the extensions list.

extension=php_printer.dll

Then add the following lines at the bottom of the file. This tells PHP what printer to use as a default. If the printer is locally installed then PHP only needs the name.

[printer]
printer.default_printer = "Your printer name"

If the printer is installed on another computer then you need to supply the full address. This would look something like \\server\printer.

Save this and restart the web server. You should now have access to the printer functions. Make sure your printer is on, and that you have enough printer ink and you can get started.

To use these function to need to open the printer, start a document, and then start a page. You can have more than one page by ending and starting pages. Here is the basic model you will need to get started.

// start printer
$handle = printer_open();
printer_start_doc($handle, "My Document");
printer_start_page($handle);
// create content here
// print
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);

To create some content you need to call certain functions using the printer handle as a parameter. You can print text, lines, shapes and even images using a number of different functions. To print text you need to use the printer_draw_text() function, the first parameter is the printer handle, the second is the text to be printed and the last two are the x and y coordinates of the text on the page.

printer_draw_text($handle, 'the text that will be printed', 100, 100);

Of course, you will probably want to use a font to print with, in which case use the following additions to the code.

$font = printer_create_font("Arial", 72, 48, 400, false, false, false, 0);
printer_select_font($handle, $font);
printer_draw_text($handle, 'the text that will be printed', 100, 100);
printer_delete_font($font);

Drawing a line is nice and easy

$pen = printer_create_pen(PRINTER_PEN_SOLID, 30, "123fde");
printer_select_pen($handle, $pen);
 
printer_draw_line($handle, 1, 10, 1000, 10);
// draw more lines if you want...
 
printer_delete_pen($pen);

These are just two examples, there are a number of other functions available on the PHP Printer page.

Categories: PHP Tags: , , , , ,

Edit Any Web Page With JavaScript

November 11th, 2008 No comments

If you want to directly edit any page that you are looking at then copy and paste the following JavaScript into your address bar.

javascript:document.body.contentEditable='true' document.designMode='on' void 0

I have created a handy little bookmarklet that you can use to do the same thing.

Edit

This code has been tested in IE7, Firefox and Chrome and works well in all three.

Of course, it goes without saying that you don’t actually edit the site, just the content that you see on the screen. Hitting refresh will remove any editing that you have done. This little tool is ideal if you want to see what different content would look like on the page without having to edit HTML. It can also be used to create spoof pages with false content.