I am trying to convert .docx files to pdf using PHP on a Windows server. I tried several of the solutions from other posts, including phpdocx (which does a very poor conversion that doesn't keep any formatting) and php's Com object. I only have Office 2003, so there is no pdf converter available using Com.
I thought of using OpenOffice/LibreOffice, but haven't found any information about installing and using Com for these on a windows server (I know it can be installed, but I can't figure out how to set up Com for it).
Using a webservice is not an option due to the data on the forms (they have to remain on our server). This means that Zend Framework cannot be used.
Any suggestions would be helpful, or information about using Com with Open Office.
I was finally able to get this working. OUr problem was that Word 2003 didn't have a PDF converter in it. We ended up using a trial version of Office 2010 for now (assuming that everything works correctly, we will purchase the full version). Word 2007 would have worked also. Below is the code I used to get this working:
//Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application') or die('MS Word could not be loaded');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0;
$word->DisplayAlerts = 0;
try{
$doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "doc opened";
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "created pdf";
$word->Quit();
$word = null;
Related
Using the following code:
// get the file contents
$file = new Google_Service_Drive_DriveFile();
$fileId = 'The File ID';
try {
echo 'try part <br>';
$result3 = $service->files->get($fileId);
echo 'Contents: ' . $result3->getContent().'<br>';
echo "Title: " . $result3->getTitle() . '<br>';
echo "Description: " . $result3->getDescription() . '<br>';
echo "MIME type: " . $result3->getMimeType() . '<br>';
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
};
Everything works except for this line:
echo 'Contents: ' . $result3->getContent().'<br>';
In the drive.php file in the API, there is a getContent() function, but it's not working. How do I get the content out of the file?
If you refer to the Google documentation at https://developers.google.com/drive/v2/reference/files/get you'll see that there are two forms of the Get method. The default fetches the metadata (eg. title, description, etc), whereas alt=media fetches the content. Alternatively, you can also fetch the content via the downloadUrl property. I don't know if the PHP library exposes the alt=media option (I don't like libraries), but the downloadUrl approach has an example on the page I linked, just scroll down and click "php"
I am not getting Output though i include all the files
<?php
/**
* Scan network to retrieve hosts and services information.
*/
require_once 'C:/xampp/php/pear/Net/Nmap.php';
//Define the target to scan
$target = array('127.0.0.1','localhost');
$options = array('nmap_binary' => 'C:/Program Files (x86)/Nmap');
try {
$nmap = new Net_Nmap($options);
//Enable nmap options
$nmap_options = array('os_detection' => true,
'service_info' => true,
'port_ranges' => 'U:53,111,137,T:21-25,80,139,8080',//to scan only specified ports
);
$nmap->enableOptions($nmap_options);
//Scan target
$res = $nmap->scan($target);
//Get failed hosts
$failed_to_resolve = $nmap->getFailedToResolveHosts();
if (count($failed_to_resolve) > 0) {
echo 'Failed to resolve given hostname/IP: ' .
implode (', ', $failed_to_resolve) .
"\n";
}
//Parse XML Output to retrieve Hosts Object
$hosts = $nmap->parseXMLOutput();
//Print results
foreach ($hosts as $key => $host) {
echo 'Hostname: ' . $host->getHostname() . "\n";
echo 'Address: ' . $host->getAddress() . "\n";
echo 'OS: ' . $host->getOS() . "\n";
echo 'Status: ' . $host->getStatus . "\n";
$services = $host->getServices();
echo 'Number of discovered services: ' . count($services) . "\n";
foreach ($services as $key => $service) {
echo "\n";
echo 'Service Name: ' . $service->name . "\n";
echo 'Port: ' . $service->port . "\n";
echo 'Protocol: ' . $service->protocol . "\n";
echo 'Product information: ' . $service->product . "\n";
echo 'Product version: ' . $service->version . "\n";
echo 'Product additional info: ' . $service->extrainfo . "\n";
}
}
} catch (Net_Nmap_Exception $ne) {
echo $ne->getMessage();
}
?>
if your program do not have any output, it means you have syntax error, fatal error or segment error.
change php.ini setting, enable, dispaly_errors, and error_reporting with E_ALL
usually, this is enough. you can see the error message and fix the bug.
if there still not error message, try create a simple file, only show phpinfo().
if this is ok, usually, you do not have start up error, if not, enable display_startup_errors in php.ini
if you still have no error, check your apache or nginx configure, you probably config the site wrong.
if you still have no error, congratulation! you got segment error.
use gdb to find the reason.
I'm having a problem with retrieving the results of a processed feed file using Amazon MWS API with PHP. I'm using the getFeedSubmissionResult class, to be precise. The problem is that when i use the API, as instructed by the documentation, there are no relevant data that is read by the class that i can access (or so it seems). So my question is: how do i retrieve the raw XML file that amazon sends back and store it to a file on my computer?
I've been retracing the code used by MWS and trying to find where they pull in the XML file from amazon and parse it to try and save that into a file with no luck. I'd deeply appreciate it if someone could direct me to a fix to this, and if not, then maybe a work around may be better.
So this is what i've been doing:
I used the getFeedSubmissionResultSample.php provided in the samples of MWS . Supposedly, this should give me the data that tells me how many items were processed and how many processed items were successful. But it doesn't. So I tried to do a print_r of the response variable:
function invokeGetFeedSubmissionResult(MarketplaceWebService_Interface $service,$request) {
try {
$response = $service->getFeedSubmissionResult($request);
echo "<br />Var dump here: <pre>";
print_r($response);
echo ("<pre>Service Response\n");
echo ("=============================================================================\n");
echo(" GetFeedSubmissionResultResponse\n");
if ($response->isSetGetFeedSubmissionResultResult()) {
$getFeedSubmissionResultResult = $response->getGetFeedSubmissionResultResult();
echo (" GetFeedSubmissionResult\n");
if ($getFeedSubmissionResultResult->isSetContentMd5()) {
echo (" ContentMd5\n");
echo (" " . $getFeedSubmissionResultResult->getContentMd5() . "\n");
}
}
if ($response->isSetResponseMetadata()) {
echo(" ResponseMetadata\n");
$responseMetadata = $response->getResponseMetadata();
if ($responseMetadata->isSetRequestId())
{
echo(" RequestId\n");
echo(" " . $responseMetadata->getRequestId() . "\n");
}
}
echo(" ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
} catch (MarketplaceWebService_Exception $ex) {
echo("Caught Exception: " . $ex->getMessage() . "\n");
echo("Response Status Code: " . $ex->getStatusCode() . "\n");
echo("Error Code: " . $ex->getErrorCode() . "\n");
echo("Error Type: " . $ex->getErrorType() . "\n");
echo("Request ID: " . $ex->getRequestId() . "\n");
echo("XML: " . $ex->getXML() . "\n");
echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
}
}
And the output gives me this:
Service Response
GetFeedSubmissionResultResponse
GetFeedSubmissionResult
ContentMd5
G5Sw+2ooONEZU1iQoqdEOQ==
ResponseMetadata
RequestId
f9d4be45-6710-42eb-850e-f437224f9938
ResponseHeaderMetadata: RequestId: f9d4be45-6710-42eb-850e-f437224f9938, ResponseContext: EM/RH7RHQhLSc47Tj2a2Uv2CGKEfvxaKOijjcaKeoh8dGISci3yqo9OHZs7dpLDIszJVz4Jt4z8=,9SYUaktMzcOG6UyuyhXu/kJPl0gpLeenslL2rkugDLhDYftMleRx1XIexbVWNxuYl7cO6901Foiv Kp7hvaLeAQ==, Timestamp: 2013-06-18T07:29:37.393Z
I have omitted the var_dump results because i don't know if that may pose a security issue on my part. But in any case, the var_dump didn't give any data that i could access. I have also traced the code to where the classes and their methods to see if i can access it from there but came out empty-handed.
Note that I have the proper parameters for calling in the results (i.e. the FeedSubmissionId) because I've done this with the amazon scratch pad.
Your help would be greatly appreciated! :)
regards,
Caleb
I had the same problem. The issue is that the response returns the result for you to compare the received file against to verify no corruption during transmission. To get xml response with Message you should save it to file not to php://memory. So the next code works for me fine
$filename = __DIR__.'/file.xml';
$handle = fopen($filename, 'w+');
$request = new MarketplaceWebService_Model_GetFeedSubmissionResultRequest();
$request->setMerchant(MERCHANT_ID);
$request->setFeedSubmissionId(ID_TO_CHANGE);
$request->setFeedSubmissionResult($handle);
try {
$response = $service->getFeedSubmissionResult($request);
fclose($handle);
echo ("Service Response\n");
echo ("=============================================================================\n");
echo(" GetFeedSubmissionResultResponse\n");
if ($response->isSetGetFeedSubmissionResultResult()) {
$getFeedSubmissionResultResult = $response->getGetFeedSubmissionResultResult();
echo (" GetFeedSubmissionResult");
if ($getFeedSubmissionResultResult->isSetContentMd5()) {
echo (" ContentMd5");
echo (" " . $getFeedSubmissionResultResult->getContentMd5() . "\n");
}
}
if ($response->isSetResponseMetadata()) {
echo(" ResponseMetadata\n");
$responseMetadata = $response->getResponseMetadata();
if ($responseMetadata->isSetRequestId())
{
echo(" RequestId\n");
echo(" " . $responseMetadata->getRequestId() . "\n");
}
}
echo(" ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
} catch (MarketplaceWebService_Exception $ex) {
echo("Caught Exception: " . $ex->getMessage() . "\n");
echo("Response Status Code: " . $ex->getStatusCode() . "\n");
echo("Error Code: " . $ex->getErrorCode() . "\n");
echo("Error Type: " . $ex->getErrorType() . "\n");
echo("Request ID: " . $ex->getRequestId() . "\n");
echo("XML: " . $ex->getXML() . "\n");
echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
}
the result you can find in ./file.xml file
this helped me
If you do not want to use a file. Then at the end of your try statement.
$xml = stream_get_contents($request->getFeedSubmissionResult());
That will put the xml data into $xml
I am using COM to convert dynamically created word docx to PDFs. Everything is working perfectly, but our c:\windows\temp\ directory is getting extremely large (and we have limited disk space on our C drive). Below is the code I am using to generate the PDF. Is there a way to delete the temp file that gets added to the system's temp directory after the file is closed? In addition, after the word doc is converted to pdf, we no longer need it (so it doesn't matter if deleting the tmp file corrupts the word doc).
/Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0; //don't open word gui on server
$word->DisplayAlerts = 0; //don't allow any alerts to open
//open doc
try{
$doc = $word->Documents->Open(DOC_LOCATION . "temp_doc/" . $filename . ".docx");
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "temp_pdf/" . $filename . ".pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Quit();
$word = null;
Don't say what version, but for XP- you can use a simple batch file to delete the files in \Documents and Settings\USER\Local Settings\temp\. Example:
C:
cd \Documents and Settings\USER\Local Settings\Temp
attrib -R -S -H *.* /S /D
DEL /S /F /Q *.*
Modify accordingly.
is it possible to render pdf in UTF-8. for persian or arabic characters? i dont wanna use tcpdf. it's have a performance problem during make pdf via php scripts. I will try latest version of php 5.4 and pdflib 8.0.4p2 but i cant see valid persian words.
<?php
try {
$p = new PDFlib();
/* open new PDF file; insert a file name to create the PDF on disk */
if ($p->begin_document("", "") == 0) {
die("Error: " . $p->get_errmsg());
}
$p->set_info("Creator", "hello.php");
$p->set_info("Author", "Rainer Schaaf");
$p->set_info("Title", "Hello world (PHP)!");
$p->begin_page_ext(595, 842, "");
$font = $p->load_font("Courier", "unicode", "");
$p->setfont($font, 24.0);
$p->set_text_pos(50, 700);
$p->show("Hello world!");
$p->continue_text($p->utf8_to_utf16("(says PHP) سلام جهان", 'utf16'));
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;
}
catch (PDFlibException $e) {
die("PDFlib exception occurred in hello sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
}
catch (Exception $e) {
die($e);
}
$p = 0;
The main problem is that the font Courier does not have glyphs for the arabic text you want to print. If you use a font like Arial Unicode you will see the expected result.
Another tip, use $p->set_parameter("textformat", "utf8");
then you can use utf8 directly as in $p->continue_text("(says PHP) سلام جهان x");