1

Situation: open an MSACCESS report, output to PDF and download, using PHP:

1 - Initially I used the following approach that worked on my computer:

try {
    // Create a COM object for Access Application
    $accessApp = new COM("Access.Application") or die("Unable to instantiate Access");
    
    // Open the database
    $accessApp->OpenCurrentDatabase($databasePath);

    // Open the report
    $accessApp->DoCmd->OpenReport($reportName, 5, null,$filtro);
    // Set the output file path
    $outputFilePath = 'D:\\xampp\\htdocs\\sagesp\\reports\\PCT_'.$turma.'.pdf'; 

    // Export the report to PDF
    $accessApp->DoCmd->OutputTo(3,$reportName,"PDF Format (*.pdf)", $outputFilePath, false);
    $accessApp->Quit();
    unset($accessApp);
    ...

When I put it into production, Windows Server R2 2008, Office 2010. My PC is Win10, Office2019, the reports produced on Win Server did not fit on the page. Everything worked fine on my PC.

2 - Second approach, I tried this, i thought it controlled better and it was impossible not to work (how wrong I was :( )

VBA Function, works well in both Access (I tested)

Public Function ExportReportToPDF(ByVal reportName As String, ByVal filtro As String, ByVal turma As String)

    DoCmd.OpenReport reportName, acViewReport, , filtro, acHidden
    
    DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, "D:\xampp\htdocs\sagesp\reports\PCT_" & turma & ".pdf", False

End Function

PHP

    $turma=$_GET['turma'];
    $rpt=$_GET['rpt'];
    $databasePath = 'D:\xampp\htdocs\sagesp\SAGESP-2023-24-AEPA.mdb';

    $reportName=$rpt;
    $filtro='turma="'.$turma.'"';
    $accessApp = new COM($databasePath) or die("Unable to instantiate Access");
    $result = $accessApp->Application->Run("ExportReportToPDF",$reportName,$filtro,$turma);

Once again, it works 100% on my PC, but it doesn't work at all on Win Server, tested with localhost and remote. The database on my PC is a copy of the one on the Win Server, my Office is 2019 and the other is 2010 and the OS is different, both have the extension=php_com_dotnet.dll in php.ini I have the following error in the console: 'error:%20%3Cb%3ESource:%3C/b%3E%20Unknown%3Cbr/%3E%3Cb%3EDescription:%3C/b%3E%20Unknown' because the scheme does not have a registered handler. When I comment the line that calls the function, I have no error.

I've been struggling with this for many days, wasted time, several solutions, shit from M$, some ideas ?, I've seen permissions, do I need to register an object?

some ideas on what the problem might be, please remember, I have limited control on the Win Server. Thank you all

João

15
  • 4
    Instead of swearing against Microsoft, just read this old but still valid official statement about using Office on a server support.microsoft.com/en-us/topic/… To make it short "Microsoft does not recommend or support server-side Automation of Office" because "Office applications were never intended for server-side use". Commented Nov 25, 2023 at 16:17
  • 1
    Yup, Office doesn't do well on non-user accounts and this is explicitly unsupported. I've had some luck running the server program under an actual user account, but that requires some ability to configure the server. And that was far from stateless, expect trouble if your server needs to deal with more than 1 request per minute. Commented Nov 25, 2023 at 16:58
  • 1
    I don't see any moral lessons here, just practical ones. This works badly because it's not supposed to work at all. One point I would make is that this situation is probably made worse by not developing in the same environment you're going to deploy to. Office 2010 and 2019 likely have significant differences. And then there's the ancient operating system on the server as well. I suggest making a VM which mirrors the 2008 environment (I'll spare you the speech on it being years past end-of-life, I'm sure you know it) and building your code using that. It's more likely to work when copied over Commented Nov 25, 2023 at 20:36
  • 1
    To take a differnt route. Does your access database change? do you change it from the server? How often do you update on the server? The reason I ask is that you might have much better luck converting your mdb file to a proper SQLServer database and then connecting PHP just via PDO to do whatever reports you wish. The conversion from mdb to SQLServer is well defined and supported by Microsoft. You can use SQLServer express for free also. Might just be a nice work around for you. Commented Nov 26, 2023 at 9:54
  • 2
    Solved - the problem was always permissions, I went to the Apache service, logon and set one account with privileges. The code works well and quickly, let's see it in production now with some requests, if there are problems, I will restrict it to one report at a time, users will have to wait a few seconds, I will give news. many thanks Commented Nov 27, 2023 at 8:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.