Save File PDF Pimaco - laravel

I'm trying to generate the pdf and download it using a lib based on mpdf.
But I'm trying to output-pdf and don't download it.
The script as standard displays the file in pdf on the page but I need that instead of showing on the page the file is downloaded.
Original file
public function output(string $name = null, string $dest = null)
{
$this->pdf->WriteHTML($this->render());
$this->pdf->Output($name, $dest);
}
Modified File
public function output(string $name = null, string $dest = null)
{
$pdf_data = $this->pdf->WriteHTML($this->render());
$path = '\opt\lampp\htdocs\site\public\comprovantes'.$name.'.pdf';
$this->pdf->Output('\opt\lampp\htdocs\site\public\comprovantes', $pdf_data);
}
Not Working with this modificated

WORKING With:
public function output(string $name = null, string $dest = null)
{
$content = $this->pdf->WriteHTML($this->render());
$this->pdf->Output('/opt/lampp/htdocs/site/public/comprovantes/filename.pdf', 'F');
}

Related

laravel Backpack Original Image Name

I am using laravel backpack for my site and i need to upload some images, i copy the code from their website and works fine, but i need the original name on the images, i tried some things, but is not working.
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = "uploads";
$destination_path = 'storage/services/' .date('FY').DIRECTORY_SEPARATOR;
if ($value==null) {
Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
if (str_starts_with($value, 'data:image'))
{
$file = $value;
$filename = $this->generateFileName($file, $destination_path);
$image = InterventionImage::make($file)->orientate();
$fullPath = $destination_path.$filename.'.'.$file->getClientOriginalExtension();
$this->attributes[$attribute_name] = $fullPath;
}
protected function generateFileName($file, $destination_path)
{
$filename = basename($file->getClientOriginalName(), '.'.$file->getClientOriginalExtension());
$filename = Str::random(20);
while (Storage::disk('uploads')->exists($destination_path.$filename.'.'.$file->getClientOriginalExtension())) {
$filename = Str::random(20);
}
return $filename;
}
why value alwys take image base64
is there any way to git image original name?
As far as I know, you won't be able to retrieve the filename, as it's always a base64 file. But maybe this work around helps you:
Image fields blade file can be found at:
\vendor\backpack\crud\src\resources\views\crud\fields\image.blade.php
You can overwrite it by making a file on the same name at:
\resources\views\vendor\backpack\crud\fields\image.blade.php
You can change anything there, maybe you can add a hidden input with the file name.

Customise the filename in phpword

Is it possible to customize the filename in phpword when I want to download it?
I want that the file takes the prenoms of the exported row.
My code:
public function edit (Stagiaire $stagiaire)
{
$id = $stagiaire ->id;
$desc1 = Stagiaire::find($id);
$my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));
$my_template->setValue('id, $desc->id);
$my_template->setValue('prenoms, $desc->prenoms);
$my_template->setValue('nom, $desc->nom);
$filename = $stagiaire->prenoms;
try{
$my_template->saveAs(storage_path('templateStagiaire.docx'));
}catch (Réception $e){}
return response()->download(storage_path('".$filename.".docx));
}
Need help.
Thanks in advance.
Laravel example for downloading file:
public function edit(Stagiaire $stagiaire, $downloadName = null)
{
$id = $stagiaire->id;
$desc = Stagiaire::find($id);
$my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));
$my_template->setValue('id', $desc->id);
$my_template->setValue('prenoms', $desc->prenoms);
$my_template->setValue('nom', $desc->nom);
// save as `prenoms` filename
$filename = $stagiaire->prenoms;
try {
$my_template->saveAs(storage_path("$filename.docx"));
} catch (Reception $e) {
}
// if download name is null, then use filename
$downloadName = $downloadName??$filename;
return response()->download(storage_path("$filename.docx"))
->header('Content-disposition','attachment; filename="'.$downloadName.'"');
}
Similar Laravel example

I need webp image on Opencart 3. It is possible?

Is it possible to use webp images in opencart 3? I mean automatic generation.
Not all the browsers accept WEBP image format
but you can type a code to check if the browser accept WEBP format then you will have to make a new images cache and convert the existing images to webp
on the fir dir catalog/model/tool/image.php
add
$image_new_webp = 'cachewebp/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.webp';
after this
$image_new = 'cache/'
to check if the browser accept image.webp and to create your new images cache
in the same file: catalog/model/tool/image.php
add this code:
$gd = gd_info();
if ($gd['WebP Support']) {
if (!is_file(DIR_IMAGE . $image_new_webp) || (filectime(DIR_IMAGE . $image_new) > filectime(DIR_IMAGE . $image_new_webp))) {
$path = '';
$directories = explode('/', dirname($image_new_webp));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir(DIR_IMAGE . $path)) {
#mkdir(DIR_IMAGE . $path, 0777);
}
}
$image_webp = new Image(DIR_IMAGE . $image_old);
$image_webp->resize($width, $height);
$image_webp->save_webp(DIR_IMAGE . $image_new_webp);
}
}
before this line: $image_new = str_replace(
now you need a function to save the images in new format
on file dir: system/library/image.php
add this function:
public function save_webp($file, $quality = 90) {
if (is_resource($this->image)) {
imagewebp($this->image, $file, $quality);
imagedestroy($this->image);
}
}
before this line: public function save($file, $quality = 90) {
output the webp images format
add this function on file system/library/response.php
public function webpRebuild($output) {
$gd = gd_info();
if ($gd['WebP Support']) {
$uri = '';
if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
}
if (stripos($uri, 'admin') === false) { // admin is your dashboard url if you have different name jst change it
if (isset($_SERVER['HTTP_ACCEPT']) && isset($_SERVER['HTTP_USER_AGENT'])) {
if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false ) {
$re = '/(cache)(.*)(\.jpg|\.png|.jpeg)/U';
$subst = '$1webp$2.webp';
$this->output = preg_replace($re, $subst, $this->output);
}
}
}
}
}
before this line: private function compress($data, $level = 0)
on the same file you have to output the webpRebuild function so add this code:
$this->webpRebuild($this->output);
after this line: $output = $this
Search the OpenCart extension marketplace for "webp":
https://www.opencart.com/index.php?route=marketplace/extension&filter_search=webp
Here are a couple, both free:
https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=36308
https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=34275
The best solution is at Design Cart. The extension generates additional webp images and displays them if the user's browser supports webp. Download.

October CMS extend System/Models/File

I trying to keep original file name when using System/Models/File, I got following code to extend this model:
namespace System\Models;
class NewFile extends File { public function fromPost($uploadedFile) { if ($uploadedFile === null) { return; }
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
$this->disk_name = $this->getDiskName();
/*
* getRealPath() can be empty for some environments (IIS)
*/
$realPath = empty(trim($uploadedFile->getRealPath()))
? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName()
: $uploadedFile->getRealPath();
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
return $this;
It works with file itself, it keeps original name but problem is link to attached file is still being generated. Broke my mind but cant get this work. Can anyone elaborate how to fix it?
Oh I see it seems its try to use disk_name to generate URL
so you did well for saving an image
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
but you just need to replace one line .. just undo your changes and make this one change
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
// $this->disk_name = $this->getDiskName();
$this->disk_name = $this->file_name;
// use same file_name for disk ^ HERE
Link logic ( for referance only ) vendor\october\rain\src\Database\Attach\File.php and modules\system\models\File.php
/**
* Returns the public address to access the file.
*/
public function getPath()
{
return $this->getPublicPath() . $this->getPartitionDirectory() . $this->disk_name;
}
/**
* Define the public address for the storage path.
*/
public function getPublicPath()
{
$uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');
if ($this->isPublic()) {
$uploadsPath .= '/public';
}
else {
$uploadsPath .= '/protected';
}
return Url::asset($uploadsPath) . '/';
}
Just make disk_name also same as file_name so when file saved on disk it will use original name and when the link is generated it also use disk_name which is original file_name
now your link and file name are synced and will be same always.
if any doubt please comment.

multi image upload in custom component for joomla

I have tried http://docs.joomla.org/Creating_a_file_uploader_in_your_component but first it shows Error: 500 after some research I have removed code
"'.$session->getName().'" : "'.$session->getId().'",
"format" : "raw"
and error is gone. Now Image is not uploading anywhere (I have set path '/images/' folder) I am confuse in code for uploading image PART 5 where to use this code?
function storeImageFile()
{
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder' );
$path = 'PATH_GOES_HERE'.'designs'.DS;
$folder_permissions = "0755";
$folder_permissions = octdec((int)$folder_permissions);
//create folder if not exists
if (!JFolder::exists($path)){
JFolder::create($path, $folder_permissions);
}
$file = JRequest::getVar('design_images', null, 'files',
$count = count($file['name']);
for($i=0;$i<$count;$i++)
{
//$i is the array position of the $_FILES array
if(empty($file['tmp_name'][$i]))
{
return false;
}
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name'][$i]);
//setting source and destination
$temporary_name = $file['tmp_name'][$i];
$filename = str_replace(' ', '_', $file['name'][$i]);
$dest = $path.$filename;
if(JFile::upload($temporary_name, $dest))
{
echo "File Upload Successful";
return true;
}
}
}

Resources