2

我在 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."}'; 
    }
?>
4

1 回答 1

1

.NET 中有很多选项可以实现您想要做的事情。

如果您对 .NET 没有任何经验,我建议您做一个教程来帮助您入门。在 C# .NET 中开发 Web 应用程序非常简单。只需谷歌 ASP.NET C# 教程。在您在 .NET 中完成第一个 Web 应用程序并希望使用库将 HTML 转换为 PDF 后,您可以查看这篇文章,您会发现几个选项。

只是为了帮助你更多,我建议你做的第一件事是下载一个免费版本的 Visual Studio,如果你还没有安装它。Visual Studio 是一个完整的 IDE,您在开发时不需要服务器,因为它带有自己的网络服务器,所以当您编写一些代码时,您只需单击“运行”按钮,它就会被执行。

最后,当您开发应用程序时,您可以使用我不推荐的Mono Project在 Linux 服务器上发布它。或者您可以将它安装在 Windows 服务器(无论您拥有什么版本)中,微软中的 Web 服务器称为 IIS,因此您将使用 IIS 代替 Apache,配置起来也非常简单。

希望这可以帮助你

于 2012-12-18T12:51:56.707 回答