File upload preview image for .doc,.pdf - drupal

In drupal when i am uploading a .doc,.pdf files I need to display the preview of the whole document after it get uploaded may i know the answer

I've solved this issue with php, here are the functions that I wrote:
//requires imagemagick which is on most servers
function thumbFromPDF($path, $page=0){
$im = new imagick($path.'['.$page.']');
$im->setImageFormat('png');
$im->writeImage( '/tmp/img.png' );
$im=imagecreatefrompng('/tmp/img.png');
return $im;
}
function thumbFromDoc($path, $page=0){
$cmd='unoconv --server localhost --port 2002 --stdout -f pdf '.$path;//-f could be pdf, txt, or html
$pdf = shell_exec ( $cmd );
$outfilefile='/tmp/pdf.pdf';
if (! $handle = fopen ( $outfilefile, 'w' )) {
die("Cannot open file ($outfilefile)");
return false;
}
// Write $somecontent to our opened file.
if (fwrite ( $handle, $pdf ) === FALSE) {
die("Cannot write to file ($location$file)");
return false;
}
fclose ( $handle );
return thumbFromPDF($outfilefile,$page);
}
Read this article for more information:
http://www.lampdeveloper.co.uk/linux/converting-doc-to-pdf-txt-or-html-using-php-and-linux.html

Related

Local JSON file in Custom WordPress Plugin is not loading my field groups?

I am having issues loading my custom fields from my local .json file within my plugin, Saving the fields work fine but the loading does work for my other field groups, am I missing something?
// ********** JSON File Path Save ACF Fields (this works fine!) **********
add_filter('acf/settings/save_json', 'my_acf_json_save_point');
function my_acf_json_save_point( $path ) {
// update path HAVING ISSUES!!!
$path = plugin_dir_path( __FILE__ ) . 'includes/acf/acf-json';
// return
return $path;
}
// ********** JSON File Path Load ACF Fields (this does not work??? ) **********
add_filter('acf/settings/load_json', 'my_acf_json_load_point');
function my_acf_json_load_point( $paths ) {
// Remove original path
unset( $paths[0] );// Append our new path
$paths[] = plugin_dir_path( __FILE__ ) . 'includes/acf/acf-json';
return $paths;
}

How do I upload PowerPoint slideshow files in Wordpress?

I have some PowerPoint slideshow files, .ppsx, with mime-type application/vnd.openxmlformats-officedocument.presentationml.slideshow, that I want to upload to WordPress. However, when I try to upload it to the media browser, I get the error "Sorry, this file type is not permitted for security reasons.".
This is despite the fact that .ppsx files are in the list of allowed file types and mimetypes.
When you upload a file, WordPress does some security checks on the file in the wp_check_filetype_and_ext function in wp-include/functions.php:2503. Part of these checks is to validate the given mimetype of the file with the mimetype that PHP detects, using the PHP function finfo_file().
However, finfo_file() isn't always accurate, and its results are often OS dependent. In the specific case of .ppsx files, finfo_file() can read the mimetype as application/vnd.openxmlformats-officedocument.presentationml.presentation. WordPress sees this as a potential security risk because it doesn't match the given mimetype for that file extension and shuts down the upload.
wp_check_filetype_and_ext() also has a filter, and we can use this to our advantage:
function my_check_filetype_and_ext( $info, $file, $filename, $mimes, $real_mime )
{
if ( empty( $check['ext'] ) && empty( $check['type'] ) )
{
$secondaryMimetypes = ['ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation'];
// Run another check, but only for our secondary mime and not on core mime types.
remove_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
$info = wp_check_filetype_and_ext( $file, $filename, $secondaryMimetypes );
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
}
return $info;
}
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
In vanilla WordPress, there is no way to have multiple mimetypes for a single filetype. The above filter runs the mimetype checks again for a secondary set of filetype/mimetype pairs if it fails the first set of pairs. By allowing .ppsx files with the presentation mimetype, you can now upload .ppsx files!
You need to add some code in your configure.php file to upload any type of
define( 'ALLOW_UNFILTERED_UPLOADS', true );
Add this in your configure.php file and you will be able to upload any file format.
or you can follow this link
Following code work fine for me.
function wcAddCustomFileTypeAndExt( $info, $file, $filename, $mimes, $real_mime )
{
if (strpos($filename, '.ppsx') !== false)
{
$info['ext'] = 'ppsx';
$info['type'] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
}
return $info;
}
add_filter('wp_check_filetype_and_ext','wcAddCustomFileTypeAndExt', 99, 5 );

Python script not found on server

I am working on a Wordpress plugin embedding a Python script. I have uploaded the Python script to my server, and at first I got an error message on the web page, where my script is to be run, saying "permission denied" (I have redirected stderr to stdout to enable me to see the error messages). When I added execution permissions I instead get a "not found" message (done via Filezilla).
I can see the script is on the server in the correct folder, and it was apparently found earlier when the permissions were wrong. How can I get my plugin to find the Python file?
Here is the relevant PHP function in the plugin:
function embed_python( $attributes )
{
$data = shortcode_atts(
[
'file' => 'hello.py'
],
$attributes
);
$handle = popen( __DIR__ . '/' . $data['file'] . ' 2>&1', 'r' );
$read = '';
while ( ! feof( $handle ) )
{
$read .= fread( $handle, 2096 );
}
pclose( $handle );
return $read;
}
Turns out it was as simple as the server I was using (Loopia) needed the Python script in a certain folder to be able to find/run it, as well as several settings changed, see this Swedish page: https://support.loopia.se/wiki/python/.

Download file does not exist

I build a csv export using the admin folder. The file is well uploaded into my public folder.
But when I try to download it using the return, I have an error:
The file "/public/exportCSV.csv" does not exist
I can't understand why, I hope you have an idea. Thanks. I'm under Symfony 4.
$admins = $userRepository->findByRole(User::ROLE_ADMIN);
$filename='exportCSV';
$extension='csv';
$request = Request::createFromGlobals();
if($request->query->get('exportCSV')!= null){
$output = fopen($filename.'.'.$extension, 'w');
fputcsv($output, array("Id","Nom","Prénom","Activé","Dernière connexion","Date d'inscription","Url avatar","Email","Username"));
foreach ($admins as $admin){
$id=$admin->getId();
$lastname=$admin->getLastName();
$firstname=$admin->getFirstName();
$activeState=$admin->getActiveState();
if($activeState){
$active='Oui';
}else{
$active='Non';
}
$lastConnectedAt=$admin->getLastConnected();
if($lastConnectedAt==null){
$lastConnected=" ";
}else{
$lastConnected=$lastConnectedAt->format('Y-m-d H:i:s');
}
$createdAt=$admin->getCreatedAt();
if($createdAt==null){
$created=" ";
}else{
$created=$createdAt->format('Y-m-d H:i:s');
}
$urlAvatar=$admin->getUrlAvatar();
$mail=$admin->getEmail();
$username=$admin->getUsername();
$csvLine= array($id,$lastname,$firstname,$active,$lastConnected,$created,$urlAvatar,$mail,$username);
fputcsv($output,$csvLine);
}
return $this->file('/public/'.$filename.'.'.$extension);
}
You must use the correct path to webserver "public" dir. Check https://stackoverflow.com/a/48585423/3497902
In your example, you can do same like ...
$publicDir = $this->getParameter('kernel.project_dir') . '/public/'; # Your controller must extend AbstractController
$output = fopen($publicDir . $filename.'.'.$extension, 'w');

PHP - Excel renders PDF without image

I'm developing with the yii2 framework. I need to render some reports which should have some images. Everything is working in my excel file. But in PDF there are no images.
What I have in excel:
What I have in PDF:
My test code looks like this:
public function run($format = self::EXCEL) {
$this->format = $format;
if ($this->format == self::PDF) {
$rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = Yii::getAlias('#vendor/mpdf/mpdf/');
if (!\PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath)) {
throw new BadRequestHttpException('Export pdf fail');
}
$this->headerContentType .= 'pdf';
$this->headerFilename .= date('d_m_Y') . '.pdf';
} elseif ($this->format == self::EXCEL) {
$this->headerContentType .= 'vnd.ms-excel';
$this->headerFilename .= date('d_m_Y') . '.xls';
} else {
throw new Exception('Unknown format for export');
}
$this->objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $this->objPHPExcel->getActiveSheet();
$activeSheet->setTitle('Sample' . date('d_m_Y'));
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorksheet($activeSheet);
$activeSheet->getColumnDimension('B')->setWidth(50);
$activeSheet->getRowDimension(2)->setRowHeight(80);
$activeSheet->setCellValue('A2','img -> ');
$activeSheet->setCellValue('B2',' ');
$objDrawing->setCoordinates('B'.2);
$objDrawing->setOffsetX(10)->setOffsetY(10);
$objDrawing->setName('Sample_image');
$objDrawing->setDescription('Sample_image');
$objDrawing->setPath('/home/vladimir/projects/temp/img.jpg');
$objDrawing->setWidth(50)->setHeight(50);
header($this->headerContentType);
header($this->headerFilename);
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($this->objPHPExcel, $this->format);
$objWriter->save('php://output');
exit;
}
In this string:
$objDrawing->setPath('/home/vladimir/projects/temp/img.jpg');
I should write a relative path to "web" directory of my project and also I should have this image into "web" directory.
$objDrawing->setPath('img/img.jpg');
/path_to_project/web/img/img.jpg
I was stuck in CI3 and if you are using CI put the images folder outside of application folder and add path as
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('uploads/organizations/1.jpg');
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($this->excel_reader_writer->getSheetByName($download_section));
The File Structure is
/- application
/- system
/- user_guide
/- uploads

Resources