这是针对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);
,
这在一定程度上取决于远程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对其进行测试,但现在我假设保护程序包含完整的生成的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-comments,希望可以清楚地了解发生了什么,总体上,您应该执行以下步骤:
- 从Ajax请求中获取发布的数据
- 将内容写入临时文件(= pdf文件)
- 连接到ftp服务器
- 上传该文件
如果您的ftp服务器需要ssl,请查看ftp_ssl_connect
。
如果在连接/登录php时出现错误,则应该发出警告,因此请检查您的错误日志
本文链接:https://www.f2er.com/3137312.html