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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top