我在 php 中编写了一个脚本,用于从 html 生成 pdf。基本上它是 html 字符串,将其保存为 .html 文件,最后在 phantomjs 中呈现页面,并使用 imagemagick 将 pdf 连接到一个文件中。现在我需要让它为企业人员工作,这意味着将其重写为 C# 并使其可在 MS 服务器上运行。我从来没有做过任何 C# 编程,也没有使用过 .net、asp 等,所以我需要一些资源如何以及从哪里开始。我应该使用什么作为服务器,如何设置它,然后如何在 C# 中为 web 编写代码。
这是我的 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."}';
}
?>