Laravel : save a docx as PDF - laravel-5

I'm pretty new to laravel and I'm currently having some difficulties with documents. my problem as follows:
I have .docx document that I modify using \PhpOffice\PhpWord\TemplateProcessor, after that I need to convert the resulted document to a pdf file in order to display it on my view.
I tried Mpdf but I noticed that it doesnt work with docx :
public function topdf($bill){
$Mpdf = new Mpdf();
$sourceFile = storage_path( $bill);
$pageCount = $Mpdf->SetSourceFile($sourceFile);
for ($i = 1; $i <= ($pageCount); $i++) {
$Mpdf->AddPage();
$import_page = $Mpdf->ImportPage($i);
$Mpdf->UseTemplate($import_page);
$Mpdf->WriteText(75, 73.3, 'hello');
}
$file_name = 'app/public/' . Str::random(18) . '.pdf';
$Mpdf->Output(storage_path($file_name),Destination::DOWNLOAD);
}
If $bill is a file.docx this doesn't work, but if it's a file.pdf it imports it to another new pdf.
I'm stucking in this problem it's been a while. Any help please

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.

spatie / laravel-sitemap | Adding new url replacing the previous file completely

I am using spatie/laravel-sitemap package to generate sitemap. I am able to generate sitemap nicely but when i try to add some new url it is replacing the previous urls completely. I am using the code below. Any suggestion would be very much appreciated.
$keywords = Keyword::all();
$sitemap = SitemapGenerator::create(config('app.url'))
->getSitemap();
$location = Location::where('id', $location_id)->select('id', 'name', 'city_id')->with('city:id,name')->first();
foreach ($keywords as $keyword) {
$url = Url::create("/toptechnicians/{$keyword->keyword}/{$location->city->name}/{$location->name}");
$url2 = Url::create("/topbusinesses/{$keyword->keyword}/{$location->city->name}/{$location->name}");
$sitemap
->add($url
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1))
->add($url2
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1))
->writeToFile(public_path('sitemap.xml'));
}

Add username as watermark when downloading an already existing pdf file in codeigniter

What I want to achieve is that whenever a user downloads a pdf file from the system, a watermark with his/her Username is added to the file. I have had a look at this link but I haven't got around it (it uses native PHP).
You can use for the watermark downloaded file the below code.
public function doWaterMark()
{
$currentFile = $this->file;
//$currentFile = put here your file;
$Username = "UserName";
$this->pdf = new FPDI();
$pagecount = $this->pdf->setSourceFile($currentFile);
for ($i = 1; $i <= $pagecount; $i++) {
$this->pdf->addPage(); //<- moved from outside loop
$tplidx = $this->pdf->importPage($i);
$this->pdf->useTemplate($tplidx, 10, 10, 100);
// now write some text above the imported page
$this->pdf->SetFont('Arial', 'I', 40);
$this->pdf->SetTextColor(255, 0, 0);
$this->pdf->SetXY(25, 135);
$this->_rotate(55);
$this->pdf->Write(0, $Username);
$this->_rotate(0); //<-added
}
$this->pdf->Output('New File Name', 'F');
}

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;
}
}
}

Get original image url Magento (1.6.1.0)

I have the following piece of code:
$cProduct = Mage::getModel("catalog/product");
foreach($products_id as $product_id) {
$product = $cProduct->load($product_id);
//$products_image[] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"", $product);
//$products_image[] = $product->getMediaConfig()->getMediaUrl($_product->getData('image'));
$products_image[] = $product->getImageUrl(); //product's image url
}
As you can see, I've tried several ways to get the original image url. Currently I'm using getImageUrl(), but it retrieves the base image, which is a cropped version. How can I retrieve the original image??
Thanks in advance.
Edit:
Seriously appears there isn't a function for it, been Googling for hours straight (also before posting here). So I've written my own function.
function get_original_image_url($base_image_url) {
$exploded = explode("/", $base_image_url);
$image_name = $exploded[count($exploded) - 1];
$original_image_url = "http://yoursitehere.com/media/catalog/product/" . $image_name[0] . "/" .
$image_name[1] . "/" . $image_name;
return $original_image_url;
}
I call it with:
$original = get_original_image_url($product->getImageUrl());
Works for me, though it isn't a nice way to do it.
You should use the catalog product media config model for this purpose.
<?php
//your code ...
echo Mage::getModel('catalog/product_media_config')
->getMediaUrl( $product->getImage() ); //getSmallImage(), getThumbnail()
Hope this helps.
Other faster way:
$cProduct = Mage::getModel("catalog/product");
$baseUrl = Mage::getBaseUrl('media') . 'catalog/product/';
foreach($products_id as $product_id) {
$product = $cProduct->load($product_id);
$products_image[] = $baseUrl . $product->getImage();
}

Resources