1

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.

Jeff
  • 45
  • 1
  • 8

2 Answers2

1

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>
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • I edited my question, that js prints the entire page, I need to print just what that php gives me. – Jeff Feb 28 '13 at 21:28
  • Use a print stylesheet to hide the unwanted content http://stackoverflow.com/questions/355313/how-do-i-hide-an-element-when-printing-a-web-page/356123#356123 – Mike B Feb 28 '13 at 21:28
0

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:

https://github.com/ceeram/CakePdf

thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • Thanks a lot for your reply, In the end I chose for doing it the simple way using js and css media print. – Jeff Mar 01 '13 at 16:28
  • That's fine, From your original question, it wasn't clear "what kind of content" was inside 'Content.text'. It could have been a full HTML page, in which case you need to render it as a standalone page, without layout, therefore my suggestions. – thaJeztah Mar 01 '13 at 16:30