I generated a PDF via Html2Pdf but i have no clue how i can save that file on my FTP Server and not localy.
I already tried it with JSPDF but the problem with that library was that i couldn't save the element css. So i am trying to make it work with 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);
});
I know that it is possible to pass this data to a "upload.php" file but i dont know how to achieve that.
I would really appreciate any help.
this is for html2pdfrocket.com
You can try following way
Assign following to $value and then use
<!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( array( 'apikey' => $apikey, 'value' => $value, '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);
It depends a little bit on the remote FTP server, so maybe you need a little bit Try&Error to get the connection working
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);
});
I have not tested it with Html2Pdf, but for now I assume saver contain the full generated pdf-bibary
The JS will send the file-Content to a upload.php
file on the server
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);
}
?>
See inline-comments, hopefully it's clear what's going on, overall you should follow these steps:
In case your ftp server requires ssl look into ftp_ssl_connect
. In case of errors while connecting/login php should raise warnings, so probably check your error log
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.