This commit is contained in:
2026-03-19 16:55:24 +08:00
parent 3d47d20fb8
commit f86247c930
3 changed files with 48 additions and 3 deletions

View File

@@ -54,7 +54,16 @@ func (l *DownloadReportPdfLogic) DownloadReportPdf(req *types.DownloadReportPdfR
}
resp = &types.DownloadReportPdfResp{
FileName: fmt.Sprintf("report_%s.pdf", req.OrderNo),
FileName: func() string {
fileKey := req.OrderNo
if fileKey == "" {
fileKey = orderId
}
if fileKey == "" {
fileKey = "unknown"
}
return fmt.Sprintf("report_%s.pdf", fileKey)
}(),
Content: pdfBytes,
}
return resp, nil

View File

@@ -31,8 +31,16 @@ func (l *DownloadReportPdfLogic) DownloadReportPdf(req *types.DownloadReportPdfR
return nil, fmt.Errorf("生成报告 PDF 失败: %w", err)
}
fileKey := req.OrderNo
if fileKey == "" {
fileKey = req.OrderId
}
if fileKey == "" {
fileKey = "unknown"
}
resp = &types.DownloadReportPdfResp{
FileName: fmt.Sprintf("report_%s.pdf", req.OrderNo),
FileName: fmt.Sprintf("report_%s.pdf", fileKey),
Content: pdfBytes,
}
return resp, nil

View File

@@ -40,7 +40,8 @@ func (s *ReportPDFService) GenerateReportPDF(ctx context.Context, orderId, order
ctxt, cancel := chromedp.NewContext(ctx)
defer cancel()
ctxt, timeoutCancel := context.WithTimeout(ctxt, 40*time.Second)
// 渲染 + 滚动触发加载 + 打印需要更充裕时间,避免长报告截断
ctxt, timeoutCancel := context.WithTimeout(ctxt, 90*time.Second)
defer timeoutCancel()
var pdfBuf []byte
@@ -48,6 +49,33 @@ func (s *ReportPDFService) GenerateReportPDF(ctx context.Context, orderId, order
chromedp.Navigate(reportURL),
// 等待报告主体区域渲染完成(数据加载并成功渲染后才会出现)
chromedp.WaitVisible(`.pc-report-body`, chromedp.ByQuery),
// 额外等待:确保至少渲染出一个模块块
chromedp.WaitVisible(`.pc-product-block`, chromedp.ByQuery),
// 某些模块可能按视口/IntersectionObserver 懒加载,打印前滚动到页底触发渲染
chromedp.ActionFunc(func(ctx context.Context) error {
// 逐步滚动到页面底部,避免一次性跳转导致部分观察器不触发
var height int64
if err := chromedp.Evaluate(`Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)`, &height).Do(ctx); err != nil {
return err
}
step := int64(900)
if height < step {
step = height
}
for y := int64(0); y <= height; y += step {
js := fmt.Sprintf(`window.scrollTo(0, %d);`, y)
if err := chromedp.Evaluate(js, nil).Do(ctx); err != nil {
return err
}
time.Sleep(120 * time.Millisecond)
}
// 回到顶部,确保页眉与首屏布局稳定
if err := chromedp.Evaluate(`window.scrollTo(0, 0);`, nil).Do(ctx); err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
return nil
}),
chromedp.ActionFunc(func(ctx context.Context) error {
buf, _, pdfErr := page.PrintToPDF().
WithPrintBackground(true).