0

我在这里得到了这个代码https://codepen.io/practicalPDF/pen/eYBOpEm 基本上,在对 pdf 进行注释或评论之后,它可以帮助您下​​载新的 pdf。我尝试修改它,以便当我保存评论的 pdf 时,它会转到我的服务器而不是下载到我的计算机上。不工作

<script type="text/javascript">
var path = "<?php echo $last_file ?>"
var urlToPDF =
"https://docsdemedard.com/"+path;
const clientId = "dbd96f4342ab4e92b822ac5fd166d737";
const viewerOptions = {
embedMode: "FULL_WINDOW",
showDownloadPDF: false,
showPrintPDF: false,
showAnnotationTools: true
};

const saveOptions = {
autoSaveFrequency: 0,
enableFocusPolling: false,
showSaveButton: true
}

function fetchPDF(urlToPDF) {
return new Promise((resolve) => {
    fetch(urlToPDF)
        .then((resolve) => resolve.blob())
        .then((blob) => {
            resolve(blob.arrayBuffer());
        })
})
}

function hideLink() {
document.getElementById("getFile").style.display = "none";
}

function updateSaveUI(zipFileName) {



document.getElementById("getFileText").innerHTML = "You can retrieve your saved file from: <a onclick='hideLink(); return true;'' href='https://practicalpdf.com/code-pens/reflect/uploads/" + zipFileName + "'>here.</a>";
document.getElementById("getFile").style.display = "flex";
}

document.addEventListener("adobe_dc_view_sdk.ready", function () {
// Create embedded view
var adobeDCView = new AdobeDC.View({
    clientId: clientId,
    locale: "fr-FR",
    divId: "adobe-dc-view"
});


adobeDCView.registerCallback(
AdobeDC.View.Enum.CallbackType.SAVE_API,
function (metaData, content, options) {
    var uint8Array = new Uint8Array(content);
    var blob = new Blob([uint8Array], { type: 'application/pdf' });
    formData = new FormData();
    var pdfFilename = urlToPDF.split("/").slice(-1)[0];
    pdfFilename = pdfFilename.split(".")[0] + "-" + uuidv4() + ".pdf";
    formData.append('pdfFile', blob, pdfFilename);

    //var zipFileName = pdfFilename.replace(".pdf", ".zip");


    fetch("https://practicalpdf.com/code-pens/reflect/", {
        method: 'POST',
        body: formData,
    })
        .then(
            function (response) {
                if (response.status == 200) {
                    updateSaveUI(pdfFilename);
                    //alert(pdfFilename);

                }
            }

            

        )

    return new Promise((resolve, reject) => {
        resolve({
            code: AdobeDC.View.Enum.ApiResponseCode.SUCCESS,
            data: {
                metaData: { fileName: urlToPDF.split("/").slice(-1)[0] }
            }
        });
    });
},
saveOptions
);


// Show the file
var previewFilePromise = adobeDCView.previewFile(
    {
        content: { promise: fetchPDF(urlToPDF) },
        metaData: { fileName: urlToPDF.split("/").slice(-1)[0] }
        ,  

    },
    viewerOptions
);


});

//试图通过ajax解决它

$.post('newfile.php', {pdfFile: formData});

// 辅助函数:

function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
});
}

(function () {
if (Blob.arrayBuffer != "function") {
    Blob.prototype.arrayBuffer = myArrayBuffer;
}

function myArrayBuffer() {
    return new Promise((resolve) => {
        let fileReader = new FileReader();
        fileReader.onload = () => {
            resolve(fileReader.result);
        };
        fileReader.readAsArrayBuffer(this);
    });
}
})();
//这是我的服务器文件
require_once "conn.php";
$file = $_POST['pdfFile'];
$target_dir = "uploads/";
move_uploaded_file($file,$target_dir);

谁能告诉我我做错了什么以及如何解决?

4

1 回答 1

0

2号线应该...

$file = $_FILES["pdfFile"]["tmp_name"];

我的那个 CodePen 的服务器端 PHP 看起来像这样......

<?php
header('Access-Control-Allow-Origin: *'); 
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["pdfFile"]["name"]);

$target_zip = str_replace(".pdf", ".zip", $target_file);

if (move_uploaded_file($_FILES["pdfFile"]["tmp_name"], $target_file)) {
    $zip = new ZipArchive;
    if ($zip->open($target_zip, ZipArchive::CREATE) === true) {
        $zip->addFile($target_file, basename($_FILES["pdfFile"]["name"]));
        $zip->close();
    }
}
?>
于 2021-11-11T15:43:49.953 回答