问题
i have following problem: I have a web-application for signing pdf-files in the browser.
A user can upload a pdf file which will be saved as blob-file in a sql-database. An other user can now digital sign the file. This works fine but I'm in trouble to send the signing data back to PHP to upgrade the blob file in the database with the siging data. So that the next user can sign the file another time.
This is my way to store the file into the database (upload_system_va.php):
$filedata = base64_encode(file_get_contents($_FILES['upl_file']['tmp_name']));
$dbcon = new PDO(...);
$insert_file = $dbcon->prepare(
"UPDATE document SET doc_blob = :blob, `status` = :status WHERE did = :did"
);
$insert_file->bindParam(':blob', $filedata, PDO::PARAM_LOB);
$insert_file->bindParam(':status', $status);
$insert_file->bindParam(':did', $did);
$insert_file->execute();
So i can show the pdf-file in the browser (viewer.php?did=49):
<?php
require_once("../controller/upload_system_va.php");
$id = intval($_GET['did']);
$PDF = mysqli_fetch_array(get_fileContent($id));
$PDFdata = $PDF['doc_blob'];
?>
...
<div class="viewer">
<object id="viewer" name="view" data="data:application/pdf;base64,<?php echo $PDFdata ?>" type="application/pdf"></object>
</div>
...
By clicking the sign button the user signs the document. I use the following library for the signing-process: https://github.com/Communication-Systems-Group/pdfsign.js
This is my code in signing.js:
$('#sign_load_btn').click(function () {
var data = document.getElementById("viewer").data;
data = convertDataURIToBinary(data);
pdfSigned = PDFSIGN.signpdf(data, certInput, document.getElementById('password').value);
sendDataToPHP(pdfSigned);
})
function sendDataToPHP(pdfSigned) {
//TODO
}
I tried some things to solve the problem for example:
function sendDataToPHP(pdfSigned) {
if (pdfSigned != 0) {
$.post("viewer.php", {
view: true,
pdfdata: pdfSigned,
},
function (data) {
if (data != "") {
alert("data sending success");
} else {
alert("data sending NOT success");
}
}
);
}
}
Or code i found in web:
function sendDataToPHP(pdfSigned) {
var base64str = encodeBase64(pdfSigned);
var binary = atob(base64str.replace(/\s/g, ''));
var len = binary.length;
var buffer = new ArrayBuffer(len);
var viewA = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
viewA[i] = binary.charCodeAt(i);
}
var blob = new Blob([viewA], {type: "application/pdf"});
$.ajax({
url: 'http://localhost/mythesis/src/controller/upload_syste_va.php',
data: blob,
type: 'POST',
contentType: false,
processData: false,
view: true,
success: function(data) {
alert("data sending success");
}
});
}
in PHP I get an array
. The type of pdfSigned in JS is object
.
I tried to restore the data in PHP (uploaded_system_va.php) :
if(isset($_POST['view'])) {
signedDoc_to_db();
}
function signedDoc_to_db() {
$pdfdata = $_POST['pdfdata'];
//$pdfdata = $_POST['data'];
$did = 49;
//$signed_file = base64_encode(serialize($pdfdata));
$signed_file = base64_encode($pdfdata);
$dbcon = new PDO(...);
if (!$dbcon) {
die('<p>Fehler beim Verbinden mit der Datenbank!</p>');
}
$insert_signedFile = $dbcon->prepare(
"UPDATE document SET doc_blob = :pdfdata WHERE did = :did"
);
$insert_signedFile->bindParam(':pdfdata', $signed_file, PDO::PARAM_LOB);
$insert_signedFile->bindParam(':did', $did);
$insert_signedFile->execute();
}
When i try it in this way, i store a invalid blob file with a size of 80 byte (I'm expacting 300 KiB for a valid signed pdf file) in the database.
I hope, i could explain my problem well and that somebody could help.
Greetz and sorry for my bad english
ilikefatcats
来源:https://stackoverflow.com/questions/60812052/store-pdf-object-data-from-js-as-blob-in-database-with-php