我通过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”文件,但我不知道如何实现。
我将非常感谢您的帮助。
答案 0 :(得分:0)
这是针对html2pdfrocket.com
您可以尝试以下方式
将以下内容分配给$ 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);
答案 1 :(得分:0)
这在一定程度上取决于远程FTP服务器,因此也许您需要一些Try&Error才能使连接正常工作
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对其进行测试,但现在我假设保护程序包含完整的生成的pdf-bibary
JS将文件内容发送到服务器上的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-comments,希望可以清楚地了解发生了什么,总体上,您应该执行以下步骤:
如果您的ftp服务器需要ssl,请查看ftp_ssl_connect
。
如果在连接/登录php时出现错误,则应该发出警告,因此请检查您的错误日志