I have written a script in php that is used for generating pdf’s out of html. Basically it get’s the html string, saves it as a .html file and finally renders the page in phantomjs and joins the pdfs to one file with imagemagick. Now I need to have it working for the enterprisey people, which means rewriting this to C# and making it runnable on MS servers. I’ve never did any C# programming, nor used .net, asp etc so I need some resources how and where to start with. What should I use as a server, how to setup it and then how to code in C# for the web.
This is my php script :
<?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."}';
}
?>
There are plenty of options in .NET to achieve what you are trying to do.
If you have no experience in .NET whatsoever I would suggest you doing a tutorial to get you started. Is quite simple to develop a web application in C# .NET. Just google ASP.NET C# Tutorial.
After you get your first web application done in .NET and want to use a library to transform HTML to PDF, you can check this post you will find several options.
Just to help you out more, the first thing I suggest you to do is to Download a free version of Visual Studio if you don’t already have it installed. Visual Studio is a complete IDE and you will not need a server while you are developing because it comes with its own webserver, so when you write some code you just click the Run button and it will be executed.
Finally, when you get your application developed you can however publish it on a Linux Server using Mono Project which I do not recommend. Or you can install it in a Windows server (whatever version you have) and the Web Server in microsoft is called IIS, so instead of Apache you will use IIS, is also really simple to configure.
Hope this helps you