I am trying to follow a tutorial on converting doc to pdf using openoffice. I have the following code:
<?php
set_time_limit(0);
function MakePropertyValue($name, $value,$osm){
$oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
}
function word2pdf($doc_url, $output_url){
// Invoke the OpenOffice.org service manager
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
// Set the application to remain hidden to avoid flashing the document onscreen
$args = array(MakePropertyValue("Hidden",true,$osm));
// Launch the desktop
$top = $osm->createInstance("com.sun.star.frame.Desktop");
// Load the .doc file, and pass in the "Hidden" property from above
$oWriterDoc = $top->loadComponentFromURL($doc_url,"_blank", 0, $args);
// Set up the arguments for the PDF output
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
// Write out the PDF
$oWriterDoc->storeToURL($output_url,$export_args);
$oWriterDoc->close(true);
}
$output_dir = './';
$doc_file = './test.docx';
$pdf_file = 'DpmR5Reqv1.20.pdf';
$output_file = $output_dir . $pdf_file;
$doc_file = 'file:///' . $doc_file;
$output_file = 'file:///' . $output_file;
word2pdf($doc_file,$output_file);
?>
I get the error:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `com.sun.star.ServiceManager' in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
( ! ) com_exception: Failed to create COM object `com.sun.star.ServiceManager': Invalid syntax in C:\wamp\www\Projects\doc_to_pdf\index.php on line 11
Ive tried to what this tutorial suggests: http://puno.ayun.web.id/2009/08/php-ooo-in-microsoft-windows-environment/ But no luck. Any idea what I can do? I am running this under wamp and it will be ran under wamp in production.
You have to have OpenOffice setup to run as a service on that machine. To simply convert an odt to a pdf you can use pyodconverter. They also explain how to setup the local OpenOffice service:
http://www.artofsolving.com/opensource/pyodconverter
I'm using this technique in a script I wrote and have an article for here:
http://codeuniversity.com/scripts/scr1
Please install Open Office in your Directory. OpenOffice setup to run as a service on that machine.
it is much easier to use headless libreoffice and a php wrapper class like https://github.com/ncjoes/office-converter.
of course you have to install libreoffice and you must have full control of your webserver.
Related
I want to convert a webpage to PDF in PHP using the library wkhtmltopdf. I'm using this PHP wrapper mikehaertl/phpwkhtmltopdf available here. I downloaded wkhtmltopdf binary from here and I used the below PHP code to point to it :
require __DIR__.'/vendor/autoload.php';
$pdfName = 'Invoice1.pdf';
$pdf = new mikehaertl\wkhtmlto\Pdf("some HTML code here");
$pdf->binary = __DIR__.'/wkhtmltopdf_0.12.4/bin/wkhtmltopdf';
if (!$pdf->send($pdfName, true)) {
echo $pdf->getError();
}
I'm getting this error: sh: 1: /var/www/mysite/public_html/wkhtmltopdf_0.12.4/bin/wkhtmltopdf: not found
I don't understand why it is saying file not found. I tried to check with PHP if the file exists using file_exists and it returns true when I provide to it that location.
Any help please?
Thanks.
I am running in a Windows10 environment. My php script writes a pdf file and I simply want to have the file open up in the pdf viewer that I specify. I am using the "runAsynchronously" function given in the PHP manual and I have tried many variations. I have no problem getting the process to run in the background - it appears every time in my TaskManager process listing, but no window appears - what am I doing wrong? If I double click the link file that has been written it works fine. It is nothing to do with the path to the executable or the filename - I can replace the pdf viewer with "notepad.exe" and the $file with a suitable text file - the same thing happens, notepad appears as a process, but not as a window, and the link works fine.
Here are some code snippets
$cmd = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
runAsynchronously($cmd, $file, 7, null, true);
function runAsynchronously($path, $arguments, $windowstyle=1, $lnkfile=null, $exec=true) {
$tmp = (is_null($lnkfile)) ? 'C:\temp\temp.lnk' : $lnkfile;
try {
if(file_exists($tmp)) { unlink($tmp); }
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut($tmp);
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$waitforcompletion = false;
if($exec) {
// Run kicks off the process in the background, but no window gets opened
$oExec = $WshShell->Run($tmp, $windowstyle, $waitforcompletion);
unlink($tmp);
} // if not executed link is left available for manual running
unset($WshShell,$oShellLink,$oExec);
} catch(Exception $ex) {
print $ex->getMessage();
}
}
If opening in the browser a pdf or downloading it solves your problem, try a class called PDFMerger:
https://github.com/clegginabox/pdf-merger
It uses an API called setasign FPDI and FPDF (https://packagist.org/packages/setasign/fpdi-fpdf#p-456)
I tested it here, because it also interested me and worked ok. (Wamp in Windows 8.1)
I put everything together(classes and folders of fpdi and fpdf) in a folder called pdfmerger because I was having some difficulty with namespaces and my code worked ok, but I know this is not the best way to include the classes - the example in GitHub page must help if you have problems with classes not finding classes):
<?php
include "pdfmerger/fpdf.php";
include "pdfmerger/fpdi.php";
include 'pdfmerger/PDFMerger.php';
$pdf = new PDFMerger();
//generate a pdf with the pages 1,2 and 3 and send to browser
$pdf->addPDF('originalpdfsavedintheserver.pdf', '1,2,3')
->merge('browser', 'nameinbrowser.pdf');
// REPLACE 'browser' WITH 'file', 'download', 'string', or 'browser' for output options
If you use the option 'download' you can choose the program that will open the pdf file in your windows settings(Choose default program to open pdfs):
https://www.cnet.com/how-to/how-to-set-default-programs-in-windows-10/
I want to create directory and sub directory in azure storage from php and get that directory. This is my code.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connection_string);
$content = fopen("abc.txt", "r");
$blob_name = "dir1/dir2/di3/myblob";
try {
//Upload blob
$blobRestProxy->createBlockBlob("mycontainer", $blob_name, $content);
}
catch(ServiceException $e){
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/library/azure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
I want to get directory "dir1".I checked that example How to create a sub container in azure storage location.
But it's not in php. Can any one help me how can we create and get directory in azure from PHP code.
You can use ListBlobsOptions to set specially object when querying Blobs from container. Per your requirement, you can use the setDelimiter('/') of this options to set the delimiter as to separate the virtual directory from blob names.
And to use getBlobPrefixes() to get the virtual directory names from ListBlobsResult which is the result of listBlobs() function.
Please consider the following code snippet:
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
$listBlobsOptions = new ListBlobsOptions();
$listBlobsOptions->setDelimiter('/');
$result = $blobClient->listBlobs(<container_name>, $listBlobsOptions);
foreach ($result->getBlobPrefixes() as $k => $b) {
//an instance of MicrosoftAzure\Storage\Blob\Models\BlobPrefix
array_push($return['blobPrefixes'], $b->getName());
}
And here is a sample about how to use the modules in Azure Staoge SDK for PHP. You can have a full glance of the usage from https://github.com/Azure-Samples/storage-blob-php-webapplication/blob/master/PHP/api/trial.php#L163.
Any further concern, please feel free to let me know.
I have a php script that uses imagick to get image colors among other things.
php /home/username/public_html/cron.php
I would get this error on the scheduled run
[30-Apr-2013 00:00:02] PHP Fatal error: Class 'Imagick' not found
in /home/username/public_html/cron.php on line 113
A sample of the php code:
try {
$image = new Imagick($filename);
$image->scaleImage(1,1);
$pixel = $image->getImagePixelColor(1, 1);
$color = $pixel->getColor();
$image_r = $color['r'];
$image_g = $color['g'];
$image_b = $color['b'];
$image->destroy();
} catch (ImagickException $e) {
// something went wrong, handle the problem
$image_r = 0;
$image_g = 0;
$image_b = 0;
}
Do I need set up some environmental variable so it knows where to look for imagick?
It works fine if I invoke the php script from the browser. Hopefully I can move this script out of the public_html when I have it working so I don't get a third party calling it.
thanks.
You need to set path of your imagemagick something like this used to work for me
imagemagickpath = /usr/bin/imagemagick
in some config file
and then use that imagemagickpath
Assuming it works in non cron environment, I notice your script is in the web root. how about using wget instead:
wget http://localhost/cron.php
I am trying to use PHP with word.application to read a file. It simply will not open the file. It's echoing the right version.
$w = new COM("word.application") or die("Is office installed?");
echo 'Loaded Word, version ' . $w->Version . '<br>';
$w->Visible = false;
$w->Documents->Open(realpath('test.docx'));
$content = (string) $w->ActiveDocument->Content;
echo $content;
$w->Quit();
$w->Release();
$w = null;
I get the error:
Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This command is not available because no document is open.'
It feels like it's some kind of permission problem. I tried to put the path of the test.docx besides using realpath and that did not help. Also tried to put it in the root of my C drive. I am using Windows 7 Professional and Microsoft Office 2007.
Documents->Open returns a document if all is ok. Probably, the document does not exists (path incorrect), or you haven't got the rights to open it from PHP. Store the result in $var, check if it has an appropriate value (probably not isset, null or false if not), and use $var->Content to read the content.
Try doing a file_exists on said
file/path.
If that works, try
file_get_contents and see if you can
read it.
If that all works - then it's not a problem with permissions/etc.