首页 > 解决方案 > 如何使用 PDFKit 将谷歌地图集成到 pdf 中

问题描述

我使用 nodejs 作为后端技术。我通过附加pdf发送电子邮件。我正在使用 pdfkit 生成 pdf ( https://www.npmjs.com/package/pdfkit )。如何根据 pdf 中的坐标显示谷歌地图图像?

标签: node.jsgoogle-mapspdfkitnode-pdfkit

解决方案


获得地图图像后,使用Google Maps Static获取 地图图像,您可以将其放置在 PDF 中

const PDFDocument = require('pdfkit');
const axios = require('axios');

const mapImg = `https://maps.googleapis.com/maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=${YOUR_API_KEY}`
axios.get(mapImg, {
  responseType: 'arraybuffer'
}).then((res) => {
  const doc = new PDFDocument;
  doc.image(res.data);
  doc.pipe(res);
  doc.end();
})


推荐阅读