display a barcode with Zend_PDF - zend-framework

Hy guys,
how can i display a barcode with Zend_PDF ?
this is my code:
$config = new Zend_Config(array(
'barcode' => 'code39',
'barcodeParams' => array('text' => '11020109'),
'renderer' => 'image',
'rendererParams' => array('imageType' => 'gif'),
));
$renderer = Zend_Barcode::factory($config)->render();
now how can i render it to my pdf?
i try without succes with:
$barcode = Zend_Pdf_Image::imageWithPath($renderer);
$page->drawImage($barcode, 10, 510, 290, 550);
thanks

The following should get you going, there is 3 things you have to change, your renderer, your method to render the barcode to the pdf and for some obscure reason, you have to include a font to your Zend_Barcode or you will get an error
$pdf = new Zend_Pdf();
// Your font (path might differ)
Zend_Barcode::setBarcodeFont(APPLICATION_PATH . '\..\data\resources\fonts\arial.ttf');
$config = new Zend_Config(
array(
'barcode' => 'code39',
'barcodeParams' => array('text' => '11020109'),
'renderer' => 'pdf', // here is your new renderer
'rendererParams' => array(), // you can define position offset here
)
);
$pdfWithBarcode = Zend_Barcode::factory($config)->setResource($pdf)->draw(); // your new barcode renderer is defined here, from now on to add things to your pdf you need to use the new variable ($pdfWithBarcode)
// Save your pdf (path might differ)
$pdfWithBarcode->save(APPLICATION_PATH . '\..\data\testBarcode.pdf');

Related

Kartik DEST_FILE yii2

Please I am having a problem trying to save a generated pdf file to a tmp folder using Kartik.
I have used the code but nothing seems to work, this a snippet of my code.
Thanks.
$content = $this->renderPartial('sertifikat', [
'model' => $model,
'bal' => $bal,
'derece' => $derece,
]);
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_FILE,
'filename' => Url::to(['generate/report']),
'tempPath' => '/generatedFolder',
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '#vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Krajee Report Header'],
'SetFooter'=>['{PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
filename should be a string, the output PDF filename (may include a path).
YOUR MISTAKE IS
'filename' => Url::to(['generate/report']),
CHANGE TO
path/to/filename.pdf

drupal form api file upload

I have a form with file attachment. Once i load the file and submit the form, if validation errors occur the form is loaded again, but the file i had uploaded is not rendered and I have to load it again.
I tried using file_save_upload but it doesnot seem to work.
$file_attach_set= file_save_upload('file_attachment1', array());
//$file_attach_setII = $form_state['values']['attc'];
$contextid = 150;
if(empty($file_attach_setII)){
$form['file_attachment' . $i] = array(
'#type' => 'file',
"#title" =>'kik'
'#default_value'=> $file_attach_set->fid,
//'#title_display' => $file_attach_set->uri,
''
);
}
The file element doesn't have a #default_value property. Try using the managed_file type which has this property.
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#file
$form['file_attachment' . $i] = array(
'#type' => 'managed_file',
'#title' =>'kik',
'#default_value'=> array(
'fid' => $file_attach_set->fid,
),
);

drupal 7 presist data in form_state with managed_file

I use D7 managed File.
If i have form error than the form lost file info, i know that is there an error a have to reupload the files.
But in the form validator i have the file save in the db so i have the FId of the file (from file_load)
If I can presist value in form state i can load the file from db in form submit and make it presistent.
in form:
$form['fileUpload'] = array(
'#id' => 'fileUploadId',
//'#type' => 'file',
'#title' => t('upload a file: '),
'#size' => 22,
'#type' => 'managed_file',
'#description' => t('upload file: docx doc pdf'),
'#upload_location' => 'public://',
'#upload_validators' => array(
'file_validate_extensions' => array('docx doc pdf'),
// Pass the maximum file size in bytes
'file_validate_size' => array(4*1024*1024),
),
);
i tried:
I pass the &$form_state by reference in form validator , submit, and form
$form_state['values']['FileInfo'] = $form_state['values']['fileUpload'];
If there is form error form_state lost this value
I pass the &$form_state by reference in form validator , submit, and form
$file = file_load($form_state['values']['fileUpload']);
$form_state['values']['FileInfo'] = $form->fid;
form_set_value($element, $value, &$form_state) for persist form_state data
But it's bit tricky to use.
I found the answer.
i create a hidden field in the form :
$form['infoFile'] = array('#type' => 'hidden', '#value' => '');
in the form validator:
$file = file_load($form_state['values']['candidateCvUpload']);
$form['infoFile']['#parents'] = array('infoFile');
form_set_value($form['infoFile'], $file->fid, $form_state);
submit
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);

Custom Error Message for Captcha Element In Zend Framework 1.10

I am trying to set my own custom error message onto my Captcha but for some reason it is echoing twice.
Here is my captcha code:
$captcha = new Zend_Form_Element_Captcha(
'captcha', // This is the name of the input field
array('captcha' => array(
// First the type...
'captcha' => 'Image',
// Length of the word...
'wordLen' => 6,
// Captcha timeout, 5 mins
'timeout' => 300,
// What font to use...
'font' => 'images/captcha/font/arial.ttf',
// URL to the images
'imgUrl' => '/images/captcha',
//alt tag to keep SEO guys happy
'imgAlt' => "Captcha Image - Please verify you're human"
)));
And then to set my own error message:
$captcha->setErrorMessages(array('badCaptcha' => 'My message here'));
When the validation fails I get:
'My message here; My message here'
Why is it duplicating the error and how do I fix it?
After spending a LOT of time trying to get this to work, I've ended up setting the messages in the options of the constructor
$captcha = new Zend_Form_Element_Captcha(
'captcha', // This is the name of the input field
array(
'captcha' => array(
// First the type...
'captcha' => 'Image',
// Length of the word...
'wordLen' => 6,
// Captcha timeout, 5 mins
'timeout' => 300,
// What font to use...
'font' => 'images/captcha/font/arial.ttf',
// URL to the images
'imgUrl' => '/images/captcha',
//alt tag to keep SEO guys happy
'imgAlt' => "Captcha Image - Please verify you're human",
//error message
'messages' => array(
'badCaptcha' => 'You have entered an invalid value for the captcha'
)
)
)
);
I looked into this answer, but I didn't really like this solution, Now I have done it using an inputspecification like:
public function getInputSpecification()
{
$spec = parent::getInputSpecification();
if (isset($spec['validators']) && $spec['validators'][0] instanceof ReCaptcha) {
/** #var ReCaptcha $validator */
$validator = $spec['validators'][0];
$validator->setMessages(array(
ReCaptcha::MISSING_VALUE => 'Missing captcha fields',
ReCaptcha::ERR_CAPTCHA => 'Failed to validate captcha',
ReCaptcha::BAD_CAPTCHA => 'Failed to validate captcha', //this is my custom error message
));
}
return $spec;
}
I just noticed, this was a question for ZF1
This is the answer for ZF2

Zend page caching similar to smarty?

I there a way to make zend_cache treat front end view similar to smarty? I would like to reduce load times and page caching seems the best way todo this.
Also it would need something similar to {nocache}.
Okay so I now have: Bootstrap.php
protected function _initCache() {
$this->bootstrap('locale');
$locale = $this->getResource('locale');
$front = array ( 'lifetime' => 1800,
'automatic_serialization' => false,
'caching' => true,
'cache_id_prefix' => 'ec_',
'debug_header' => true,
'default_options'
=> array ('cache_with_get_variables' => true,
'cache_with_post_variables' => false,
'cache_with_session_variables' => false,
'cache_with_cookie_variables' => false ),
);
$back = array('cache_dir' => '../data/Cache/'.$locale);
$cache = Zend_Cache::factory('Page', 'File', $front, $back);
$cache->start();
Zend_Registry::set('cache', $cache);
return $cache;
}
However, the only time my cache is hit is with code like:
$cache = Zend_Registry::get('cache');
if (!$data = $cache->load('sidebar_'.$module.'_'.$controller)) {
$data['Studio'] = Eurocreme_Studio::load_by_type(array('type' => 'sidebar', 'from' => 0, 'to' => COUNT_HIGH));
$data['Movie'] = Eurocreme_Movie::load_by_type(array('type' => 'sidebar', 'from' => 0, 'to' => 5));
$data['Gallery'] = Eurocreme_Gallery::load_by_type(array('type' => 'sidebar', 'from' => 0, 'to' => 5));
$data['Category'] = Eurocreme_Category::load_tree(0);
$cache->save($data, 'my_view_helper_sidebar_'.$module.'_'.$controller);
}
I was hoping to capture the entire views.
Does anyone have any working examples of how to implement it fully? The docs don't really go in-depth.
You may want to use Zend_Cache_Frontend_Output or Zend_Cache_Frontend_Page. From Zend Framework manual:
Zend_Cache_Frontend_Output is an output-capturing frontend. It utilizes output buffering in PHP to capture everything between its start() and end() methods.
Zend_Cache_Frontend_Page is like Zend_Cache_Frontend_Output but designed for a complete page.
You are probably looking for Zend_Cache_Frontend_Page. Please refer to Zend Cache docs for details.