空白 PDF 正在通过 Google Drive API 上传到 Google Drive

Blank PDFs being uploaded to Google Drive via Google Drive API

我正在尝试使用 pdfkit and google drive's api 将生成的 pdf 上传到我的 google 驱动器文件夹,但每次上传时它都是一个空白文档,尽管它与生成的标签大小相同。我有一个单独的端点,当我调用 createLabel 函数时我 returns doc.pipe(res) 并且我如何知道 pdf 已正确生成,但是当我尝试发送它时我的 upload-to-googledrive.js ,文件已成功上传为 blank 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");
                }
            }
        );
    }

当我看到doc时,我以为是流对象。所以我想提出以下修改。请修改createLabel函数如下

发件人:

return await getStream(doc);

收件人:

return doc;

参考: