1

I have a problem with converting docx to pdf files in my script.

At first I tried to use a pure php-based solution, described here: https://stackoverflow.com/a/20035739/12812601

Unfortunatelly this does not work (it creates an empty com object, then throws a fatal error).

So I've tried to use a python script to do this. I use a great script from here: https://stackoverflow.com/a/20035739/12812601

So here is a problem.

The Python script standalone (run via a command line) works just fine, and saves the converted pdf. Unfortunatelly when I try to call it via PHP it can't save a converted file.

PHP scripts can create and write files oin the same directory without any problem

This supposed to be a local configuration, so I do not care about any portability

Scripts:

*******PHP*******

<?php
//Script only for testing Python calls, tried different methods

error_reporting(E_ALL);
echo '<h1>Begin</h1>';


echo '<h2>Before call</h2>';
exec ('python dp.py');

echo '<h2>After exec call</h2>';
system('python dp.py');
echo '<h2>After Sys Call</h2>';
passthru('python dp.py');
echo '<h2>After Pass Call</h2>';
$w = get_current_user();
var_dump($w);
?>

*****Python*****

import sys
import os
import comtypes.client
import win32com.client

wdFormatPDF = 17

#static file names for testing

in_file = 'C:\\Users\\fake_user\\OneDrive\\Stuff\\f1.docx'
out_file = 'C:\\Users\\fake_user\\OneDrive\\Stuff\\f3.pdf'
print('BEGIN<br>\n')

word = win32com.client.Dispatch('Word.Application')
word.Visible = False
doc = word.Documents.Open(in_file)
print('\nOpened Docx\n<br>')
print(in_file);
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
print('\nSaved\n<br>')
doc.Close()
word.Quit()
print('DONE\n')

*****Output from the browser*****

Begin
Before call
After exec call
BEGIN
Opened Docx
C:\Users\fake_user\OneDrive\Stuff\f1.docx
After Sys Call
BEGIN
Opened Docx
C:\Users\fake_user\OneDrive\Stuff\f1.docx
After Pass Call
string(5) "fake_user"

System configuration

Windows 7 Professional Edition Service Pack 1

Apache/2.4.26 (Win32)

OpenSSL/1.0.2l

PHP/7.1.7

Python 3.8.1

I tried to run Apache both as a system service and as a user who owns the OneDrive (name changed to "fake_user" here), so it shouldn't be a permissions issue (I think)

Any help appreciated

0 Answers0