Wordpress, Combining ContuttoPDF with Code Snippet Plugin to get your code sample printed on the PDF

Posted by Daniel - 956 Views

Customizing ContuttoPDFI also use Code Snippet plugin. It’s a plugin to provide syntax highlight for programming code within the post. I want the formatted code produced by the Code Snippet is also printed in the PDF output with the same appearance as what Code Snippet does on the web page.

But unfortunately it won’t happen automatically, the ContuttoPDF does not recognize the Code Snippet formatting request tag when producing the PDF file, so you need to modify the code to get the intended result. Here is the list of steps you can follow to get the Code Snippet format printed on the PDF file:

1. Navigate to /wp-content/pdf-themes/default/ then open the pdf.php file with your text editor. Find this line of code within the file, if it has not changed yet it will be the line 135:

$pdf_output .= ‘<p class="entry">’ .wpautop( $post->;post_content ) . ‘</p>’;

2. With the assumption you had the Code Snippet plugin installed, please change the line of code mentioned above with the following code:

$CodeSnippet = new CodeSnippet();
$pdf_output .= ‘<p class="entry">’ .wpautop($CodeSnippet->;highlightCode($post->;post_content)) . ‘</p>’;
unset($CodeSnippet);

The trick is to create a new instance of Code Snippet and then pass the post_content to the highlight_code() function. You can save your pdf.php file now.

3. Open your style-print.css file with your text editor. style-print.css is located on the same path as above: /wp-content/pdf-themes/default/, then paste this CSS styling on the file:

.codesnip-container {font-family:courier;border:1px solid #ccc; background:#eee; padding: 5px;margin:10px;}
.codesnip { text-align:left; direction: ltr; }
.codesnip .de1, .php-brief .de2, .php .de1, .php .de2 {font-weight:normal; background:transparent;color:#000; }
.codesnip .kw1, .php .kw1 {color: #a1a100;}
.codesnip .kw2, .php .kw2 {color: #000; font-weight: bold;}
.codesnip .kw3, .php .kw3 {color: #000066;}
.codesnip .kw4, .php .kw4 {color: #f63333;}
.codesnip .co1, .codesnip .co2, .codesnip .coMULTI, .codesnip .co1, .codesnip .co2, .codesnip .coMULTI {color: #808080;}
.codesnip .es0, .php .es0 {color: #000033; font-weight: bold;}
.codesnip .br0, .php .br0 {color: #66cc66;}
.codesnip .st0, .php .st0 {color: #ff0000;}
.codesnip .nu0, .php .nu0 {color: #ff33ff;}
.codesnip .me0, .php .me0 {color: #006600;}

Ok Done! Save the style-print.css then have it tested. Hope everything goes well, if you find anything which maybe incorrect please let me know - I could be wrong to sometimes :D

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008


Leave a Reply


Options for your comment:





Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.

Coffee Break

Comments - Thanks Guys :)

  • Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
  • Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
  • Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.