I am having a hard time to get this done, what I need is, print a contract that is on my database, when I say print, is physical printing.
I can access the content I want to print using this
<?php echo @$contrato['Content']['text']; ?>
but I don't what else to do, like, get this info, save to a temp file and then open and print? I'm lost.
I know if I use:
<script>window.print()</script>
it will print the entire page, which I don`t want, I would like to print just what that php code brings me.
I don't what else to do, like, get this info, save to a temp file and then open and print?
Print it to the page and use this javascript to print it from the browser:
<?php echo @$contrato['Content']['text']; ?>
<script>window.print()</script>
If the content is plain-text, create a view, containing only the content of the 'text' field and disable the layout for that view/action, or don't use a view at all, but just echo the result from your controller e.g.
in your controller
public function printable($id)
{
// (retrieve your data from the database here)
// ...
$this->layout = false;
$this->autoRender = false;
echo $contrato['Content']['text'];
}
Within your existing/other view, link to the printable version;
<?php
echo $this->Html->link(
'printable version',
array(
'action' => 'printable',
0 => $id // needs to be present as a viewVar or some other way
),
array('target' => '_blank')
);
?>
The use will need to use CTRL+P or 'print' manually
some notes/remarks
If the content is plaint text, the text is printed in a default font that the browser uses.
When printing HTML from a browser, you have NO control on the exact way the page is printed. Although CSS has various 'print' options, browsers will in most cases append headers and/or footers when printing (e.g. The URL of the page that was printed and the page-number).
The only option to have full control over the layout of the output, including font, page-size and suppressing headers/footers, is to generate a PDF file of the content. Various libraries exist to convert HTML to PDF using PHP. A nice CakePHP plugin for this can be found here: