我在php中编写了一个用于从html生成pdf的脚本。基本上它得到的是html字符串,将其保存为.html文件,最后在phantomjs中呈现页面,并使用imagemagick将pdf连接到一个文件。现在我需要让它为企业人员工作,这意味着将其重写为C#并使其在MS服务器上可运行。我从来没有做过任何C#编程,也没有使用.net,asp等所以我需要一些资源如何以及从哪里开始。我应该使用什么作为服务器,如何设置它,然后如何使用C#为网络编码。
这是我的php脚本:
<?php
function curPageURL() {
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$currentURL = curPageURL();
$break = explode('/', $currentURL);
$currentURL = str_replace($break[count($break) - 1], '', $currentURL);
$cmd = '';
$phantomOut = array();
//name of the created single PDF file
$out = '-exported'.microtime().'.';
//path were temporary pdf files will be created
$outputPath = dirname(__FILE__);
//command to run phantomjs in console/terminal
$phantomPath = 'phantomjs';
if(isset($_POST['html'])){
$html = json_decode($_POST['html'], true);
$out = $range.$out.'pdf';
$l = 0;
$myFile = $outputPath."/printHtml".microtime().".html";
$fh = fopen($myFile, 'w');
//check if file is writable
if(is_writable($myFile)){
fwrite($fh, $html[$i]['html']);
fclose($fh);
$myFile = str_replace($outputPath.'/', '', $myFile);
} else {
echo '{"success": false, "msg": "Can\'t create or read file."}';
return 0;
}
//check if phantomjs is installed and reachable
exec($phantomPath.' -v', $phantomOut);
if (empty($phantomOut)){
echo '{"success": false, "msg": "PhantomJS not installed or not reachable."}';
return 0;
}
$phantomOut = array();
//run PhantomJs with parameters : temporary html filenames sent, format of the page and page orientation
$command = $phantomPath.' '.$outputPath.'/render.js "'.$myFile.'" "'.$currentURL.'"';
exec($command, $phantomOut);
//delete temp html file
unlink($myFile[$i]);
if (file_exists($phantomOut[0]) && filesize($phantomOut[0]) > 0){
//return url of the created file
echo '{"success": true, "path": "'.$outputPath.'/'.$phantomOut[0].'"}';
} else {
echo '{"success": false, "msg": "There was some problem creating the file"}';
}
} else {
echo '{"success": false, "msg": "Error in request data."}';
}
?>
答案 0 :(得分:1)
.NET中有很多选项可以实现您的目标。
如果你没有任何.NET经验,我建议你做一个教程来帮助你入门。在C#.NET中开发Web应用程序非常简单。只是谷歌ASP.NET C#教程。 在.NET中完成第一个Web应用程序并希望使用库将HTML转换为PDF后,您可以检查this post您将找到几个选项。
为了更好地帮助您,我建议您做的第一件事是下载Visual Studio的免费版本,如果您还没有安装它。 Visual Studio是一个完整的IDE,您在开发时不需要服务器,因为它带有自己的Web服务器,因此当您编写一些代码时,只需单击“运行”按钮即可执行。
最后,当您开发应用程序时,您可以使用Mono Project在Linux服务器上发布它,我不建议这样做。或者你可以将它安装在Windows服务器(你拥有的任何版本)中,而微软中的Web服务器称为IIS,因此使用IIS代替Apache,配置也非常简单。
希望这有助于你