【问题标题】:Save Generated PDF on FTP Server将生成的 PDF 保存在 FTP 服务器上
【发布时间】:2019-11-09 04:14:15
【问题描述】:

我通过 Html2Pdf 生成了一个 PDF,但我不知道如何将该文件保存在我的 FTP 服务器上而不是本地。

我已经用 JSPDF 尝试过,但那个库的问题是我无法保存元素 css。所以我试图让它与 html2pdf 一起工作。

  $("#printer").on("click", function(e) {

  var element = document.getElementById('qrcode');
  var worker = html2pdf().from(element).save();
  var saver = html2pdf().from(element).output();

    var data = new FormData();
        data.append("data" , saver);

    var xhr = new XMLHttpRequest();
        xhr.open( 'post', 'upload.php', true ); //Post to php Script to save to server
        xhr.send(data);

    });

我知道可以将此数据传递给“upload.php”文件,但我不知道如何实现。

非常感谢任何帮助。

【问题讨论】:

  • 1) 你能在当前服务于这个网页的服务器上运行 php 代码吗(包含上面的代码)?
  • @Evil_skunk 是的,我是

标签: javascript php jquery ajax html2pdf


【解决方案1】:

这是给 html2pdfrocket.com 的

你可以试试下面的方法

将following赋值给$value,然后使用

<!DOCTYPE html>
<html>
<head>
        <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
      <title>Title</title>
      <style type='text/css'>
        body{
            width: 96%;
            margin:1% 2%;
            line-height: 2em;
            letter-spacing: 0.26mm !important;
            padding:0;
            font-family:helvetica,sans serif;
            word-wrap: break-word;
        }
         SOME CSS CODE
    </style>
</head>
<body>
SOME HTML CODE

$postdata = http_build_query( 大批( 'apikey' => $apikey, '价值' => $价值, 'MarginBottom' => '30', 'MarginTop' => '15' ) );

            $opts = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postdata
                )
            );

            $context  = stream_context_create($opts);

            // Convert the HTML string to a PDF using those parameters
            $result = file_get_contents('http://api.html2pdfrocket.com/pdf', false, $context);
            file_put_contents('url_to_save/agreement.pdf', $result);

【讨论】:

    【解决方案2】:

    这在一定程度上取决于远程 FTP 服务器,所以也许你需要一点 Try&Error 才能使连接正常工作

    你的 JS 代码:

    var data = new FormData();
    data.append("data" , saver);
    
    var xhr = new XMLHttpRequest();
        xhr.open( 'post', 'upload.php', true ); //Post to php Script to save to server
        xhr.send(data);
    
    });
    

    我还没有使用 Html2Pdf 对其进行测试,但现在我假设 saver 包含完整生成的 pdf-bibary

    JS 会将文件内容发送到服务器上的upload.php 文件

    你的upload.php

    if(isset($_POST['data'])){
    
        //$_POST['data'] will contain everything sent by ajax
        $pdfContent = $_POST['data'];
    
        //tmpFile to store the pdf - will be removed automatically
        $tmpFile = tmpfile();
        fwrite($tmpFile, $pdfContent);
    
        //connect and login to a ftp server
        $ftp_server = "ftp.xx.com";
        $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    
        //probably login with user/pw
        $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
    
    
        //upload tmp-file - choose filename on server
        if (ftp_put($ftp_conn, "fileNameOnServer.pdf", $tmpFile, FTP_ASCII)){
          echo "Successfully uploaded file";
        }
        else {
          echo "Error uploading file";
        }
    
        //close connection
        ftp_close($ftp_conn);
    }
    ?>
    

    查看 inline-cmets,希望清楚发生了什么,总体而言,您应该遵循以下步骤:

    • 从 Ajax 请求中获取发布的数据
    • 将内容写入临时文件(=pdf 文件)
    • 连接到FTP服务器
    • 上传该文件

    如果您的 ftp 服务器需要 ssl,请查看 ftp_ssl_connect。 如果在连接/登录 php 时出现错误,应该会发出警告,所以可能检查你的错误日志

    【讨论】:

    • 明天我会试试的,谢谢你的回答,虽然真的很感激?
    猜你喜欢
    • 2016-09-11
    • 2015-07-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多