This topic is locked
[SOLVED]

 Creating PDF Documents and Emailing them

9/3/2013 1:59:13 PM
PHPRunner General questions
C
CWDATA author

I have a script running off a button that picks up an 2 x HTML templates (loop set up) to fill name and address etc from current record, convert to PDF and then Email to address again selected from the current record with one other document which is already a PDF and has no fields in it to update.

When activated it starts up and then just hangs.....

When I check the Docs folder the first document has been created in PDF but the only field that has been filled in is the date.

The other two documents have not been created and no mail out has occurred.
A test script was written for me which showed the error to be a Memory problem.

I have been through all the processes and set ups to increase PHP memory limits to 128M etc but to no avail.

Is there anyone out there who has come across the problem and can enlighten me?
All the best,

Carl.

C
cgphp 9/3/2013

Enable firebug and check what errors you get when you click the button.

C
CWDATA author 9/3/2013



Enable firebug and check what errors you get when you click the button.

C
CWDATA author 9/3/2013



Enable firebug and check what errors you get when you click the button.


Hi Cristian,
The first time I ran test script I got following message: **Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to

allocate 64 bytes) in */web/sites/_p/_r/_o/

procurementgroup.co.uk/public/www/pgmanagement/plugins/html2ps/box.inline.php

  • on line*175*
    However since I raised memory limit as I said in original post the result is the same but the error message when I run the same test script has changes to the following:
    Fatal error: Out of memory (allocated 137101312) (tried to allocate 82 bytes) in /web/sites/_p/_r/_o/procurementgroup.co.uk/public/www/pgmanagement/include/phpfunctions.php on line 789**
    Does this mean progress or just moved the problem elsewhere?
    Cheers,

    Carl.

C
cgphp 9/3/2013

What is the code of the "Server" section of the button?

Sergey Kornilov admin 9/4/2013

I would suggest to increase memory limit in PHP first.
In your PHP Config file (php.ini) -- Location depends on your PHP setup. Search the file for memory_limit and change to your desired value:

memory_limit: 256M
C
CWDATA author 9/4/2013



What is the code of the "Server" section of the button?


Hi mate,

Code is as follows: -

// Put your code here.

require_once('plugins/html2ps/config.inc.php');

require_once(HTML2PS_DIR.'pipeline.factory.class.php');
//error_reporting(E_ALL);

ini_set("display_errors","1");

@set_time_limit(10000);

parse_config_file(HTML2PS_DIR.'html2ps.config');
/**

* Handles the saving generated PDF to user-defined output file on server

*/

class MyDestinationFile extends Destination {

/**

* @var String result file name / path

* @access private

*/

var $_dest_filename;
function MyDestinationFile($dest_filename) {

$this->_dest_filename = $dest_filename;

}
function process($tmp_filename, $content_type) {

copy($tmp_filename, $this->_dest_filename);

}

}
class MyFetcherMemory extends Fetcher {

var $base_path;

var $content;
function MyFetcherMemory($content, $base_path) {

$this->content = $content;

$this->base_path = $base_path;

}
function get_data($url) {

if (!$url) {

return new FetchedDataURL($this->content, array(), "");

} else {

// remove the "file:///" protocol

if (substr($url,0,8)=='file:///') {

$url=substr($url,8);

// remove the additional '/' that is currently inserted by utils_url.php

if (PHP_OS == "WINNT") $url=substr($url,1);

}

return new FetchedDataURL(@file_get_contents($url), array(), "");

}

}
function get_base_url() {

return 'file:///'.$this->base_path.'/dummy.html';

}

}
/**

* Runs the HTML->PDF conversion with default settings

*

* Warning: if you have any files (like CSS stylesheets and/or images referenced by this file,

* use absolute links (like https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=71943&image=1&table=forumreplies).

*

* @param $path_to_html String HTML code to be converted

* @param $path_to_pdf String path to file to save generated PDF to.

* @param $base_path String base path to use when resolving relative links in HTML code.

*/

function convert_to_pdf($html, $path_to_pdf, $base_path='') {

$pipeline = PipelineFactory::create_default_pipeline('', // Attempt to auto-detect encoding

'');
// Override HTML source

// @TODO: default http fetcher will return null on incorrect images

// Bug submitted by 'imatronix' (tufat.com forum).

$pipeline->fetchers[] = new MyFetcherMemory($html, $base_path);
// Override destination to local file

$pipeline->destination = new MyDestinationFile($path_to_pdf);
$baseurl = '';

$media =& Media::predefined('A4');

$media->set_landscape(false);

$media->set_margins(array('left' => 7,

'right' => 5,

'top' => 5,

'bottom' => 5));

$media->set_pixels(800);
global $g_config;

$g_config = array(

'cssmedia' => 'screen',

'scalepoints' => '1',

'renderimages' => false,

'renderlinks' => false,

'renderfields' => false,

'renderforms' => false,

'mode' => 'html',

'encoding' => 'utf-8',

'debugbox' => false,

'pdfversion' => '1.4',

'draw_page_border' => false

);
$pipeline->configure($g_config);

$pipeline->process_batch(array($baseurl), $media);

}
$fname = "";

for ( $i=0; $i<15; $i++) {

$fname .= chr(rand(97, 122));

}
$html[1] = file_get_contents( getabspath('docs/cover-letter.html') );

$html[2] = file_get_contents( getabspath('docs/terms-conditions.html') );

//$html[3] = file_get_contents( getabspath('docs/areas.html') );
$data = $button->getCurrentRecord();
for ($i=1; $i<3; $i++) {

$html[$i] = str_replace( '%ClientName%', $data['ClientName'], $html[$i]);

$html[$i] = str_replace( '%Registered Address1%', $data['RegisteredAddress1'], $html[$i]);

$html[$i] = str_replace( '%Registered Address2%', $data['RegisteredAddress2'], $html[$i]);

$html[$i] = str_replace( '%Registered Address3%', $data['RegisteredAddress3'], $html[$i]);

$html[$i] = str_replace( '%TownCity%', $data['TownCity'], $html[$i]);

$html[$i] = str_replace( '%County%', $data['County'], $html[$i]);

$html[$i] = str_replace( '%PostCode%', $data['PostCode'], $html[$i]);

$html[$i] = str_replace( '%date%', date('d-m-Y'), $html[$i]);

$html[$i] = str_replace( '%Title%', $data['Title'],$html[$i]);

$html[$i] = str_replace( '%FirstName%', $data['FirstName'], $html[$i]);

$html[$i] = str_replace( '%LastName%', $data['LastName'], $html[$i]);

convert_to_pdf( $html[$i], getabspath('docs/' . $fname . '_' . $i . '.pdf') );

}
// SENDING EMAIL
$from = "carltedd@hotmail.co.uk"; // here must be your email
$to = ($data['ClientSignatoryEmail']?$data['ClientSignatoryEmail']:"carltedd@hotmail.co.uk"); // HERE MUST BE USER eMAIL
$msg = "<h1>Dear Partner!</h1><p>Please find attached documents</p><p>Best regards</p>";
$subject="Documents";

$attachments = array();
// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:
//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type

$attachments = array(
array('path' => getabspath('docs/' . $fname . '_1.pdf'), 'name' => 'Cover-letter.pdf' ),

array('path' => getabspath('docs/' . $fname . '_2.pdf'), 'name' => 'Terms-n-Conditions.pdf' ),

array('path' => getabspath('docs/areas.pdf'), 'name' => 'Areas that we cover.pdf' )
) ;


$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'htmlbody' => $msg, 'charset' => 'UTF-8', 'attachments' => $attachments));


if(!$ret["mailed"]){

$result="FAILED:" . $ret["message"];

}

else {

unlink(getabspath('docs/' . $fname . '_1.pdf'));

unlink(getabspath('docs/' . $fname . '_2.pdf'));

$result="SENT";

}


Cheers,

Carl.

C
CWDATA author 9/4/2013



I would suggest to increase memory limit in PHP first.
In your PHP Config file (php.ini) -- Location depends on your PHP setup. Search the file for memory_limit and change to your desired value:

memory_limit: 256M



Hi there,

Thanks for your input, however I have already done this and it merely shifted the error on one file to another.

It just seems that the application at this point is leaking Memory.

Sergey Kornilov admin 9/4/2013

HTML to PDF conversion is memory hungry operation. Keep increasing memory limit until errors are gone.
Another thing - try to restrict the amount of data being converted to PDF.

C
CWDATA author 9/4/2013



HTML to PDF conversion is memory hungry operation. Keep increasing memory limit until errors are gone.
Another thing - try to restrict the amount of data being converted to PDF.


I have increased the memory to the max allowed by provider, but on all the forums I have read this should be sufficient.

The documents I am converting are simple letters with Name and Address fields to be filled in. As simple as you can really.