通过在页面末尾添加一个按钮,将我的网页内容自动转换为pdf

时间:2015-01-11 00:24:05

标签: javascript php html pdf

我想将我的网页内容转换为pdf,我希望通过在页面末尾添加一个按钮来自动生成。

我发现这个网站(http://www.web2pdfconvert.com/)为您提供了一些代码行,但它不起作用,更具体地说,我可以添加按钮,但结果是一个空的pdf页面。

这是代码......

    <html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script> 
$(function(){   
   $('#fileContents').val(document.documentElement.innerHTML);

});
</script>
<style>
div {
    width: 600px;
    border-style: solid;
    border-width: 1px;
}
</style>

</head>
<body>
<form action="download.php" method="post">
<div>
<img src="images/europass_logo.jpg" alt="EUROPASS LOGO" width="100" height="100">

<?php
$dbhost  = 'localhost';    
$dbname  = 'cvtool';       
$dbuser  = 'root';  
$dbpass  = 'smogi';   

mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());


function queryMysql($query)
{
    $result = mysql_query($query) or die(mysql_error());
     return $result;
}


    $personal_info = queryMysql("SELECT name,email,city,phone FROM personal_information WHERE username='niklakis'");
    $portfolio = queryMysql("SELECT username,portfolio,portfolio_description FROM portfolio WHERE username='niklakis'");
    $work = queryMysql("SELECT job_title,company,website,start_date,end_date,start_year,end_year FROM work WHERE username='niklakis'");
    $education = queryMysql("SELECT school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE username='niklakis'");
    $skills = queryMysql("SELECT certification,issuing_authority,start_date,end_date,start_year,end_year,description FROM skills WHERE username='niklakis'");
    $text = queryMysql("SELECT text,section_content FROM text WHERE username='niklakis'");
    if (mysql_num_rows($personal_info)&& mysql_num_rows($portfolio) && mysql_num_rows($work) && mysql_num_rows($education) && mysql_num_rows($skills))
    {
        $row = mysql_fetch_row($personal_info);

        echo "<h2>Personal Information</h2>";
        echo "Full Name: ";
        echo stripslashes($row[0]) . "<br/>Email: ";
        echo stripslashes($row[1]) . "<br clear=left /><br /> City: ";
        echo stripslashes($row[2]) . "<br clear=left /><br />Phone: ";
        echo stripslashes($row[3]) . "<br clear=left /><br />";


        //ALLAGI NA PAIRNEI TO USER P EXEI LOGAREI
        $username = 'niklakis';
        echo "<h2>Education</h2>";
        while($row = mysql_fetch_row($education))
    {
        if($username != $row[0]){
            echo "School: ";
            echo stripslashes($row[0]) . "<br/>Degree: ";
            echo stripslashes($row[1]) . "<br clear=left /><br /> Website: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />Start Date: ";
            echo stripslashes($row[3]) . "<br clear=left /><br />End Date: ";
            echo stripslashes($row[4]) . "<br clear=left /><br />Start Year: ";
            echo stripslashes($row[5]) . "<br clear=left /><br />End Year: ";
            echo stripslashes($row[6]) . "<br clear=left /><br />Degree Description: ";
            echo stripslashes($row[7]) . "<br clear=left /><br />";
            echo "_________________________<br></br>";
        }
        else
        {
            echo "School: ";
            echo stripslashes($row[0]) . "<br/>Degree: ";
            echo stripslashes($row[1]) . "<br clear=left /><br /> Website: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />Start Date: ";
            echo stripslashes($row[3]) . "<br clear=left /><br />End Date: ";
            echo stripslashes($row[4]) . "<br clear=left /><br />Start Year: ";
            echo stripslashes($row[5]) . "<br clear=left /><br />End Year: ";
            echo stripslashes($row[6]) . "<br clear=left /><br />Degree Description: ";
            echo stripslashes($row[7]) . "<br clear=left /><br />";
        }
        $username = $row[0];
    }

        //ALLAGI NA PAIRNEI TO USER P EXEI LOGAREI
        $username = 'niklakis';
        echo "<h2>Work Experience</h2>";
         while($row = mysql_fetch_row($work))
    {
        if($username != $row[0]){
            echo "Job Title: ";
            echo stripslashes($row[0]) . "<br/>Company: ";
            echo stripslashes($row[1]) . "<br clear=left /><br /> Website: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />Start Date: ";
            echo stripslashes($row[3]) . "<br clear=left /><br />End Date: ";
            echo stripslashes($row[4]) . "<br clear=left /><br />Start Year: ";
            echo stripslashes($row[5]) . "<br clear=left /><br />End Year: ";
            echo stripslashes($row[6]) . "<br clear=left /><br />";
            echo "_________________________<br></br>";
        }
        else
        {
            echo "Job Title: ";
            echo stripslashes($row[0]) . "<br/>Company: ";
            echo stripslashes($row[1]) . "<br clear=left /><br /> Website: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />Start Date: ";
            echo stripslashes($row[3]) . "<br clear=left /><br />End Date: ";
            echo stripslashes($row[4]) . "<br clear=left /><br />Start Year: ";
            echo stripslashes($row[5]) . "<br clear=left /><br />End Year: ";
            echo stripslashes($row[6]) . "<br clear=left /><br />";
        }
        $username = $row[0];
    }


        //ALLAGI NA PAIRNEI TO USER P EXEI LOGAREI
        $username = 'niklakis';
        echo "<h2>Skills</h2>";
        while($row = mysql_fetch_row($skills))
    {
        if($username != $row[0]){
            echo "Certification: ";
            echo stripslashes($row[0]) . "<br/>Issuing Authority: ";
            echo stripslashes($row[1]) . "<br clear=left /><br />Start Date: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />End Date: ";
            echo stripslashes($row[3]) . "<br clear=left /><br />Start Year: ";
            echo stripslashes($row[4]) . "<br clear=left /><br />End Year: ";
            echo stripslashes($row[5]) . "<br clear=left /><br />Degree Description: ";
            echo stripslashes($row[6]) . "<br clear=left /><br />";
            echo "_________________________<br></br>";
        }
        else
        {
            echo "Certification: ";
            echo stripslashes($row[0]) . "<br/>Issuing Authority: ";
            echo stripslashes($row[1]) . "<br clear=left /><br />Start Date: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />End Date: ";
            echo stripslashes($row[3]) . "<br clear=left /><br />Start Year: ";
            echo stripslashes($row[4]) . "<br clear=left /><br />End Year: ";
            echo stripslashes($row[5]) . "<br clear=left /><br />Degree Description: ";
            echo stripslashes($row[6]) . "<br clear=left /><br />";
        }
        $username = $row[0];
    }



        //ALLAGI NA PAIRNEI TO USER P EXEI LOGAREI
        $username = 'niklakis';
        echo "<h2>Portfolio</h2>";
    while($row = mysql_fetch_row($portfolio))
    {

        if($username != $row[0]){
            echo "Portfolio: ";
            echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />";
            echo "_________________________<br></br>";
        }
        else
        {
            echo "Portfolio: ";
            echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
            echo stripslashes($row[2]) . "<br clear=left /><br />";         
        }
        $username = $row[0];
    }



        $row = mysql_fetch_row($text);
        echo "<h2>Text</h2>";
        echo "Text Title: ";
        echo stripslashes($row[0]) . "<br/>Content: ";
        echo stripslashes($row[1]) . "<br clear=left /><br />";

    }



?>
</div>
<input type="submit" id="createPdf" value="Download PDF"/>
</form>
</body>
</html>

我得到了这个错误...... (Can't access your local URL: http://localhost/industrial/CVTool/test.php)

1 个答案:

答案 0 :(得分:4)

我发现这样做的最简单方法是使用mPDF。请注意,要执行此操作,您需要使用PHP。您不需要了解PHP,您的文件只需托管在可以访问PHP的服务器上(即从localhost运行时该文件不起作用,至少在没有PHP的情况下)。

如果您自己在localhost下托管该网站,则需要安装php。有一个很好的指南here。我相信你只需要做1-3步骤。第4步是MySQL,用于数据库交互。你不需要那个,我不相信它是必需的。

基本上,当您的页面加载时,您使用jQuery获取所有HTML并将其添加到隐藏的表单输入。当用户单击按钮时,您将表单提交到PHP页面,该页面使用HTML创建PDF页面并下载页面。实际上非常简单,mPDF完成所有繁重的任务。

这是a working example。这是一个zip file,其中包含我的示例中使用的所有文件。

获取mpdf文件夹和download.php文件,并将其放入包含HTML文件的文件夹中。然后像这样更改HTML的头部:

<script> 
 $(function(){  
     $('#fileContents').val( document.documentElement.innerHTML);
     $('#pdfForm').append('<input type="submit"  value="Download PDF" />');
 });

</script>


....
</head>

然后在你的html底部这样:

   </div>
     <form id="pdfForm" action="download.php" method="post">

            <div style="display:none;" >
              <input type="text" name="fileContents" id="fileContents" value=''/>
              <input type="text" name="fileName" id="fileName" value='mySitePage.pdf'/>
              <input type="text" name="css" value='style.css'/>

            </div>
   </form> 
  </body>
</html>