|
@@ -40,8 +40,8 @@ var (
|
|
|
Width: 1200,
|
|
|
},
|
|
|
}
|
|
|
- pdfPathTemplate = "./static/pdf/%s_%d.pdf"
|
|
|
- jpegPathTemplate = "./static/image/%s_%d.jpg"
|
|
|
+ pdfPathTemplate = "./static/%s_%d.pdf"
|
|
|
+ jpegPathTemplate = "./static/%s_%d.jpg"
|
|
|
)
|
|
|
|
|
|
type PDFParams struct {
|
|
@@ -73,8 +73,7 @@ func uploadFile(ossClient utils.OssClient, filePath string, fileType string) (re
|
|
|
}()
|
|
|
return
|
|
|
}
|
|
|
-func generateAndUploadPDF(params PDFParams, reportUrl, pdfPath string, wg *sync.WaitGroup, ossClient utils.OssClient) (resourceUrl string, err error) {
|
|
|
- defer wg.Done()
|
|
|
+func generateAndUploadPDF(params PDFParams, reportUrl, pdfPath string, ossClient utils.OssClient) (resourceUrl string, err error) {
|
|
|
if reportUrl == "" {
|
|
|
return
|
|
|
}
|
|
@@ -91,8 +90,7 @@ func generateAndUploadPDF(params PDFParams, reportUrl, pdfPath string, wg *sync.
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func generateAndUploadJPEG(params PDFParams, reportUrl, pdfPath string, wg *sync.WaitGroup, ossClient utils.OssClient) (resourceUrl string, err error) {
|
|
|
- defer wg.Done()
|
|
|
+func generateAndUploadJPEG(params PDFParams, reportUrl, pdfPath string, ossClient utils.OssClient) (resourceUrl string, err error) {
|
|
|
if reportUrl == "" {
|
|
|
return
|
|
|
}
|
|
@@ -124,34 +122,51 @@ func Report2pdfAndJpeg(reportUrl string, reportId int, ReportLayout string) (pdf
|
|
|
err = fmt.Errorf("获取OSS客户端未初始化,生成PDF失败")
|
|
|
}
|
|
|
defer func() {
|
|
|
-
|
|
|
if err != nil {
|
|
|
- utils.FileLog.Info("生成PDF失败m,error: \n" + err.Error())
|
|
|
+ utils.FileLog.Info("生成PDF失败,error: \n" + err.Error())
|
|
|
}
|
|
|
}()
|
|
|
// 并发执行
|
|
|
var wg sync.WaitGroup
|
|
|
wg.Add(2)
|
|
|
+ pdfChan := make(chan result, 1)
|
|
|
+ jpegChan := make(chan result, 1)
|
|
|
go func() {
|
|
|
- pdfUrl, err = generateAndUploadPDF(params, reportUrl, pdfPath, &wg, ossClient)
|
|
|
- if err != nil {
|
|
|
-
|
|
|
- }
|
|
|
+ defer wg.Done()
|
|
|
+ url, pdfErr := generateAndUploadPDF(params, reportUrl, pdfPath, ossClient)
|
|
|
+ pdfChan <- result{url, pdfErr}
|
|
|
}()
|
|
|
go func() {
|
|
|
- jpegUrl, err = generateAndUploadJPEG(params, reportUrl, jpegPath, &wg, ossClient)
|
|
|
- if err != nil {
|
|
|
-
|
|
|
- }
|
|
|
+ defer wg.Done()
|
|
|
+ url, jpgErr := generateAndUploadJPEG(params, reportUrl, jpegPath, ossClient)
|
|
|
+ jpegChan <- result{url, jpgErr}
|
|
|
}()
|
|
|
wg.Wait()
|
|
|
+ close(pdfChan)
|
|
|
+ close(jpegChan)
|
|
|
+
|
|
|
+ pdfResult := <-pdfChan
|
|
|
+ jpegResult := <-jpegChan
|
|
|
+ if pdfResult.err != nil || jpegResult.err != nil {
|
|
|
+ utils.FileLog.Info("生成PDF失败,error: \n" + pdfResult.err.Error() + jpegResult.err.Error())
|
|
|
+ err = fmt.Errorf("生成PDF失败,error: \n" + pdfResult.err.Error() + jpegResult.err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+type result struct {
|
|
|
+ url string
|
|
|
+ err error
|
|
|
+}
|
|
|
+
|
|
|
func ReportToPdf(width int, reportUrl, filePath string, top, bottom, left, right int) (err error) {
|
|
|
pyCode := `
|
|
|
import asyncio
|
|
|
from pyppeteer import launch
|
|
|
|
|
|
+@asyncio.coroutine
|
|
|
async def main():
|
|
|
# 异步代码
|
|
|
browser = await launch({
|