Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: [MOD] Print article to a pdf file... (sNews 1.4)  (Read 3060 times)

David

  • Hero Member
  • *****
  • Karma: 7
  • Posts: 567
[MOD] Print article to a pdf file... (sNews 1.4)
« on: March 20, 2006, 12:57:09 am »

Hello all!

This mod is to print your articles to PDF files...

I'm not a master in PHP but i have wrote this small code which works well for articles without tables and images.
It is one first draft which can be a base of work...

You can see the result of this first draft here: http://www.avenir-financier.com/sNewstest/print2pdf.php?id=11

The code:

1. Add these lines in your snews.php files:

Code: [Select]
//******************
// GLOBAL SETTINGS
//******************

//******************************
$s['pdf_font'] = "./fonts/Helvetica.afm";
$s['pdf_fontsize_title'] = "14";
$s['pdf_fontsize_date'] = "9";
$s['pdf_fontsize_article'] = "10";
//******************************


2. Download this file http://www.ros.co.nz/pdf/downloads.php?f=pdfClassesAndFonts_009e.zip and copy the files
class.ezpdf.php, class.pdf.php and the font directory in your sNews directory.

3. Create a new file ./print2pdf.php with these lines:

Code: [Select]
<?php
// Print an article to a pdf file - by Avenir
// For sNews 1.4
// 19/03/2006

include ("snews.php");
include (
"class.ezpdf.php");

function 
replace($arg)
{
$search = array("<p>""</p>",
                        
"'","","","","","",
        "<a href="",
                "
</a>",
        "" target="
_blank"",
        "
"
,
        "<strong>","</strong>",
        "<u>","</u>");

$replace = array("nn","",
         "'","","","","","",
         "<c:alink:",
         "</c:alink>",
         "",
         "",
         "<b>","</b>",
         "<c:uline>","</c:uline");

$temp str_replace($search,$replace,$arg);
return 
$temp;
}


if (!empty(
$_GET)) {
extract($_GET);
} else if (!empty(
$HTTP_GET_VARS)) {
extract($HTTP_GET_VARS);
}
if (!empty(
$_POST)) {
extract($_POST);
} else if (!empty(
$HTTP_POST_VARS)) {
extract($HTTP_POST_VARS);
}

$tab = array(
'justification'=>'left',
'spacing'=>1.2);


if(!isset(
$id)){ $id=""; }
$db mysql_connect(s('dbhost'),s('dbuname'),s('dbpass'));
  
mysql_select_db(s('dbname')) or die(s('dberror'));

if (
$id <> "") {
$query "SELECT * FROM articles WHERE id = $id";
$shorten 200000;
$result mysql_query($query);
while (
$r mysql_fetch_array($result)) {
$date date(s('rss_date_format'), strtotime($r['date']));
$article_title replace(stripslashes($r['title']));

$article_text substr(stripslashes($r['text']),0$shorten);
$pdf_code replace($article_text);

$pdf =& new Cezpdf('a4','portrait');
$pdf -> ezSetMargins(20,70,20,50);

$pdf->selectFont(s('font'));
$pdf->setColor(1,0.5,0.1);
$pdf->ezText($article_title,s('pdf_fontsize_title'),$tab);
$pdf->setColor(0,0,0);
$pdf->ezText("".$date." - ".s('website')."",s('pdf_fontsize_date'),$tab);
$pdf->setLineStyle(1);
$pdf->line(20,770,400,770);
$pdf->ezSetY(750);
$pdf->ezText($pdf_code,s('pdf_fontsize_article'));

$pdf->ezStream();

 }
}
?>



4. You can now do this : h**p://www.yoursite.com/print2pdf.php?id=10 for ex. (10 is the article id)

There are still problems with the images and tables.  But this code functions well for articles with text and links only.

Tell me what you think about this small code.


Avenir



« Last Edit: October 27, 2008, 09:11:17 am by philmoz »
Logged

bramsyuur

  • Hero Member
  • *****
  • Karma: 23
  • Posts: 873
    • http://snews.vietbee.net
[MOD] Print article to a pdf file... (sNews 1.4)
« Reply #1 on: March 20, 2006, 01:24:25 am »

Great and useful mod Avenir! ;)
Thanks and good job!
Logged
La comunidad sNews en tu idioma!
Comunidad sNews en Espaol