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
Related
How can i disable the processing of images while element is being saved? when i edit the html of a text-element and add an image with a relative path, the editor creates an absolute path and furthermore processes the image. the image is copied to a new folder and the src-path is adjusted. i want to get rid of this behaviour. it this possible, so i can just set the src-attribute of the image and it will stay like that?
I'm using the extension "TinyMCE".
In the TCA of your extension, remove the configuration parameter 'uploadfolder'. This will lead to the behaviour that the file is referenced to the original location instead of being copied. See the TCA reference for more information.
The configuration might look like this:
'image' => array(
'exclude' => 0,
'label' => 'LLL:EXT:yourextension/Resources/Private/Language/locallang_db.xml:tx_yourextension_domain_model_yourmodel.image',
'config' => array(
'type' => 'group',
'internal_type' => 'file',
'show_thumbs' => 1,
'size' => 1,
'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
'disallowed' => '',
// removing/commentingthe next line will create a reference instead of copying the file
// 'uploadfolder' => 'uploads/tx_yourextension', //
),
),
I'm fighting with functional testing of files uploading.
I will try to simplify my situation. Let's say I have
a company entity, which has 3 fields.
Company {
protected name;
protected tags;
protected images;
}
Images is array of CompanyImage entities, which serve for
storing image files and tags contains array of Tag entities,
which can be m:n connected with companies.
In the form I use jquery, for adding tags and images dynamically.
(you can create images and add them to the company similar to the
collection type symfony tutorial)
Because images and tag arrays are created with jquery, I cannot
simply use something like the turorial line below in the functional test of the company form.
$form['images'][0]->upload('/path/to/image.jpg');
For the setting
of the form values I use simple a little trick described by sstok here
(https://github.com/symfony/symfony/issues/4124)
public function testCompanyCreation() {
...
//option1
$image = new UploadedFile(
'/path/to/image.jpg',
'image.jpg',
'image/jpeg',
123
);
//or option2
//$image = array('tmp_name' => '/path/to/image.jpg', 'name' => 'image.jpg', 'type' => 'image/jpeg', 'size' => 300, 'error' => UPLOAD_ERR_OK);
$companyFormNode = $companyCrawler->selectButton('Create');
$companyForm = $companyFormNode->form();
$values = array(
'company' => array(
'_token' => $companyForm['company[_token]']->getValue(),
'name' => 'test company',
'tags' => array('1'),
'images' => array('0' => (array('file' =>$image))),
),
);
$companySubmitCrawler = $client->request($companyForm->getMethod(), $companyForm->getUri(), $values, $companyForm->getPhpFiles());
}
this works perfectly until I try to upload the image file.
With the option1 I get following exception
Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
when I use option2 I get this
Argument 1 passed to Acme\myBundle\Entity\CompanyImage::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in ...\PropertyAccess\PropertyAccessor.php on line 347 and defined (500 Internal Server Error)
I would also like to point out, that the whole form and uploading of the files works without any problems in the browser. I also tried to make the entities serializable, and it didn't help. Do I have a bug somewhere?
I have figured it out (took couple of hours). Files have to be uploaded in a separate array.
$companyForm = $companyFormNode->form();
$values = array(
'company' => array(
'_token' => $companyForm['company[_token]']->getValue(),
'name' => 'test company',
'tags' => array('1')
),
);
$files = array(
'company' => array('images' => array('0' => (array('file' => $image))))
);
$companySubmitCrawler = $client->request(
$companyForm->getMethod(),
$companyForm->getUri(),
$values,
$files
);
I am working with files in a drupal module. I added this form
$form['Background_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
'#upload_location' => 'public://backgroundimage/',
'#default_value' => $this->options['Background_image'],
);
the file is added properly, once the file is uploaded a remove button appears allwoing to remove the file and upload a new one.The problem is that this button is not working so I looked into file module and I found that the hook of removing a file is not implemented yet.
function file_file_delete($file) {
// TODO: Remove references to a file that is in-use.
}
How can I remove an uploade file?
you should use file_delete()
more description here: https://api.drupal.org/api/drupal/includes%21file.inc/function/file_delete/7
I will be able to help you more if you post here module version,
maybe it will start working after update :-)
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);
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');