|
@@ -0,0 +1,43 @@
|
|
|
+import domtoimage from 'dom-to-image';
|
|
|
+import JsPDF from 'jspdf';
|
|
|
+import './simfang-normal';
|
|
|
+/**
|
|
|
+ * @param ele
|
|
|
+ * @param pdfName
|
|
|
+ * */
|
|
|
+function createPDF(ele, pdfName, other) {
|
|
|
+ // 生成base64图片码
|
|
|
+ domtoimage
|
|
|
+ .toPng(ele, {})
|
|
|
+ .then(function (dataUrl) {
|
|
|
+ console.log('生成成功');
|
|
|
+ getPDF(ele, dataUrl, pdfName, other);
|
|
|
+ })
|
|
|
+ .catch(function (error) {
|
|
|
+ console.error('生成失败', error);
|
|
|
+ });
|
|
|
+}
|
|
|
+function getPDF(ele, url, name, other) {
|
|
|
+ // a4纸大小
|
|
|
+ var imgWidth = 595;
|
|
|
+ var imgHeight = 842;
|
|
|
+ var pdf = new JsPDF('', 'pt', 'a4');
|
|
|
+ // 添加字体
|
|
|
+ pdf.setFont('simfang');
|
|
|
+ // 水印
|
|
|
+ if (other && other.is_water == true) {
|
|
|
+ const pageHeight = pdf.getPageHeight();
|
|
|
+ pdf.saveGraphicsState();
|
|
|
+ pdf.setGState(pdf.GState({ opacity: 0.5 }));
|
|
|
+ pdf.setFontSize(18);
|
|
|
+ pdf.text(other.water, 100, pageHeight / 2, 42);
|
|
|
+ pdf.text(other.water, 110, pageHeight / 1.5, 42);
|
|
|
+ pdf.restoreGraphicsState();
|
|
|
+ }
|
|
|
+ pdf.addImage(url, 'JPEG', 0, 0, imgWidth, imgHeight);
|
|
|
+ pdf.save(name);
|
|
|
+}
|
|
|
+
|
|
|
+export default {
|
|
|
+ createPDF,
|
|
|
+};
|