生成 Crystal Reports 报告的代码在 CLI 中工作正常,但“找不到数据源名称”当从浏览器访问时
我正在尝试使用 Crystal Reports 从 PHP 生成报告, 代码似乎是正确的:
<?php
set_time_limit(0);
if(isset($_GET['id']))
{
$id = $_GET['id'];
} else {
die('Please specify an ID');
}
$path = "c:\\wamp\\www\\billing\\reports";
$file = $chemin."\\bill_".$id.".pdf";
$app_obj = new COM("CrystalRuntime.Application") or Die ("Did not open");
$report= $path."\\bill.rpt";
$rpt_obj= $app_obj->OpenReport($report,1);
$app_obj->LogOnServer("p2ssql.dll","host","bdd","userbd","passwordbd");
$rpt_obj->EnableParameterPrompting = FALSE;
$rpt_obj->RecordSelectionFormula = "{F_DOCLIGNE.DO_Piece}='$id'";
$rpt_obj->ExportOptions->DiskFileName = $file;
$rpt_obj->ExportOptions->PDFExportAllPages = true;
$rpt_obj->ExportOptions->DestinationType = 1;
$rpt_obj->ExportOptions->FormatType = 31;
$rpt_obj->Export(false);
header("Content-Type: application/pdf");
readfile($file);
?>
如果我从命令行运行脚本,它工作得很好,并且我在控制台中导出并解析了 PDF。
但从浏览器的角度来看,情况有所不同, 如果您通过 Apache 收到请求,我会收到此异常:
com_exception: Source: Crystal Reports ActiveX Designer
Details : IM002:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified in C:\wamp\www\facture\report.php on line 25
在报告中,数据库连接是通过 ODBC 建立的,我无法使用它,因为任何其他标有 ODBC 的驱动程序都拒绝工作。
I'm trying to generate a report from PHP, using Crystal Reports,
The code seems to be correct:
<?php
set_time_limit(0);
if(isset($_GET['id']))
{
$id = $_GET['id'];
} else {
die('Please specify an ID');
}
$path = "c:\\wamp\\www\\billing\\reports";
$file = $chemin."\\bill_".$id.".pdf";
$app_obj = new COM("CrystalRuntime.Application") or Die ("Did not open");
$report= $path."\\bill.rpt";
$rpt_obj= $app_obj->OpenReport($report,1);
$app_obj->LogOnServer("p2ssql.dll","host","bdd","userbd","passwordbd");
$rpt_obj->EnableParameterPrompting = FALSE;
$rpt_obj->RecordSelectionFormula = "{F_DOCLIGNE.DO_Piece}='$id'";
$rpt_obj->ExportOptions->DiskFileName = $file;
$rpt_obj->ExportOptions->PDFExportAllPages = true;
$rpt_obj->ExportOptions->DestinationType = 1;
$rpt_obj->ExportOptions->FormatType = 31;
$rpt_obj->Export(false);
header("Content-Type: application/pdf");
readfile($file);
?>
If I run the script from command line, it works just fine, and I have the PDF exported and parsed in the console.
But from a browser's point of view, the story is different,
If you get the request through Apache, I get this exception :
com_exception: Source: Crystal Reports ActiveX Designer
Details : IM002:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified in C:\wamp\www\facture\report.php on line 25
On the report, the database connection is made through ODBC, I couldn't use that, as any other driver, marked with ODBC refused to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要设置一些环境变量。下面是我必须进行的设置才能使用不同的 ODBC 驱动程序(注意:在 OS X 上)。
You may have to set some environmental variables. Below is what I had to set to use a different ODBC driver (note: on OS X).
我看到你用的是Wamp。此行为很可能与运行 Web 服务器 (Apache) 的用户有关。
当您从 CLI 运行 PHP 时,PHP.exe 会继承您的用户配置文件(就数据库/网络/文件系统访问权限而言);另一方面,通常 Apache(以及 IIS)在不同的非特权凭据下运行,以防止黑客破坏和利用服务器。
在 Apache 中,您可以通过修改
httpd.conf
Apache 配置文件(在您的情况下,它可能位于C:\wamp\apache\conf
下)来更改此行为,更改User
和/或Group
配置指令。I see you are using Wamp. This behaviour is very probably related to the user that the web server (Apache) runs under.
When you run PHP from CLI, PHP.exe inherits your user profile (in terms of database/network/filesystem access rights); on the other hand, usually Apache (and IIS,too) runs under different, unprivileged credentials, to prevent hackers from damaging and exploiting the server.
In Apache, you can change this behaviour modifying the
httpd.conf
Apache config file (in your case it could be located underC:\wamp\apache\conf
), changing theUser
and/orGroup
config directives.