【问题标题】:Blank PDFs being uploaded to Google Drive via Google Drive API通过 Google Drive API 将空白 PDF 上传到 Google Drive
【发布时间】:2020-08-11 18:36:15
【问题描述】:

我正在尝试使用pdfkit 和google drive 的api 将生成的pdf 上传到我的google drive 文件夹,但每次上传它都是一个空白文档,尽管它与生成的标签大小相同。我有一个单独的端点,当我调用 createLabel 函数时我返回 doc.pipe(res) 并且我知道正确生成了 pdf,但是当我尝试将它发送到我的 upload-to-googledrive.js 时,文件成功上传为 空白 pdf。有人有什么建议吗?

create_label.js

module.exports.createLabel = async (
    fromAddress,
    toAddress,
    recipient,
    weight,
    qrCodeUrl,
    orderID
) => {
    const doc = new PDFDocument();

    // Rounded Rect is layout
    doc.roundedRect(10, 10, 590, 720, 5).stroke();
    // Fit the image within the dimensions
    doc.image("assets/rocketship.png", 40, 30, { fit: [100, 100] }).stroke();

    doc.rect(20, 20, 200, 150).stroke();
    doc.fontSize(20).font("Times-Bold").text("From:", 230, 30).moveDown(0.5);
    doc.fontSize(18).font("Times-Roman").text(fromAddress);

    doc.rect(220, 20, 370, 150).stroke();
    doc.fontSize(20).font("Times-Bold").text("To:", 30, 180).moveDown(0.5);
    doc.fontSize(18).font("Times-Roman").text(toAddress).moveDown(1);
    doc.fontSize(18).font("Times-Bold").text(recipient).moveDown(1);

    doc.rect(20, 170, 570, 300).stroke();

    doc.rect(20, 470, 270, 250).stroke();
    doc
        .fontSize(20)
        .font("Times-Roman")
        .text(`Order ID: ${orderID}`, 30, 480)
        .moveDown(0.5);
    doc.fontSize(20).font("Times-Roman").text(`Weight: ${weight}`).moveDown(0.5);

    doc.image(qrCodeUrl, 300, 440, { width: 300 });

    doc.rect(290, 470, 300, 250).stroke();

    doc.end();

    return await getStream(doc);
};


index.js

// other setup code
try {
            const {
                fromAddress,
                recipient,
                shippingPackageId,
                orderID,
                toAddress,
                weight,
            } = req.body;

            logger.info("Attempting to generate QR code");
            const qrCodeUrl = await createQR(shippingPackageId.toString());
            // create label
            logger.info("Attempting to create label");
            const stream = await createLabel(
                fromAddress,
                toAddress,
                recipient,
                weight,
                qrCodeUrl,
                orderID
            );

            logger.info("Uploading to google drive");
            uploadToDrive(orderID, stream);

            //Respond 200 if everything went well
            return res.status(200).send("Succesfully Uploaded to google drive");
        } catch (error) {
            throw res.status(500).send(error);
        }

上传到 googledrive.js

module.exports.uploadToDrive = (orderID, stream) => {
// ... google drive setup functions
/**
     * Lists the names and IDs of up to 10 files.
     * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
     */
    function uploadFile(auth) {
        const folderId = `1iAZcU12zgwWC6Iw1eln9kZGx8POhkVLT`;

        const fileMetadata = {
            name: `label_${orderID}.pdf`,
            parents: [folderId],
        };

        const media = {
            mimeType: "application/pdf",
            body: stream,
        };

        const drive = google.drive({ version: "v3", auth });

        drive.files.create(
            {
                resource: fileMetadata,
                media: media,
                fields: "id",
            },
            function (err, file) {
                if (err) {
                    // Handle error
                    throw err;
                } else {
                    console.log("file uploaded");
                }
            }
        );
    }

【问题讨论】:

  • 我能在你的脚本中问你getStream()吗?
  • 是的,当然,它来自 get-stream 库 npmjs.com/package/get-stream 在此处发表评论 stackoverflow.com/a/60346863/5979597 等待文档完成,然后再发送流
  • 检查你没有上传任何东西的流。
  • 感谢您的回复。从您的回复中,我提出了一个修改点作为答案。你能确认一下吗?当我在我的环境中测试这个修改时,我可以确认createLabel 的修改脚本有效。但是,如果这不是您问题的直接解决方案,我深表歉意。

标签: node.js google-api google-drive-api google-api-js-client


【解决方案1】:

看到doc,我还以为是流对象。所以我想提出以下修改。请修改createLabel的功能如下。

发件人:

return await getStream(doc);

收件人:

return doc;

参考:

【讨论】:

  • @Chad Nehemiah 欢迎您。谢谢你让我知道。我很高兴你的问题得到了解决。如果您的问题已解决,请按接受按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。我认为您的问题和解决方案将对他们有用。如果找不到按钮,请随时告诉我。 stackoverflow.com/help/accepted-answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
相关资源
最近更新 更多