You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure部署环境中Spring Boot后端生成PDF无法在Angular前端正常显示问题求助

Azure部署环境中Spring Boot后端生成PDF无法在Angular前端正常显示问题求助

大家好,我最近碰到一个棘手的问题,想请各位帮忙排查下:

我有一个部署在Azure Web App上的Spring Boot 2后端应用,前端是部署在Azure Static Web App的Angular项目。后端会生成PDF文件,以byte[]的形式返回,同时设置application/pdf响应头;前端拿到响应后转成Blob再展示。

这个功能在本地环境一直正常,之前部署到Azure上也能正常运行,但不知何时起在Azure环境失效了(本地依然正常)。这是个团队项目,期间代码整体做了不少变更,但PDF生成相关的功能代码并没有改动。我已经花了四天时间排查,目前唯一找到的线索是项目里新增了staticwebapp.config.json文件。

我现在需要让这个功能在Azure上恢复正常,麻烦各位帮忙出出主意,谢谢啦!

当前错误

[PDF显示错误截图]

后端代码(Azure Web App)

@Override
public ResponseEntity<byte[]> generateAndDownloadPdfFile(String templateName, Map<String, Object> data, List<String> fonts) {
    Context context = new Context();
    context.setVariables(data);
    String htmlContent = templateEngine.process(templateName, context);

    try {
        ByteArrayOutputStream target = new ByteArrayOutputStream();
        ITextRenderer renderer = new ITextRenderer();

        for (String font : fonts) {
            renderer.getFontResolver().addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        }

        fonts.stream().forEach(font -> {
            try {
                renderer.getFontResolver().addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
        });

        renderer.setDocumentFromString(htmlContent);
        renderer.layout();
        renderer.createPDF(target, false);
        renderer.finishPDF();

        byte[] bytes = target.toByteArray();
        HttpHeaders header = new HttpHeaders();
        header.add("Content-Type", "application/pdf");

        return ResponseEntity.ok()
                .headers(header)
                .contentType(MediaType.APPLICATION_PDF)
                .body(bytes);
    } catch (DocumentException e) {
        throw new InvalidArgumentException("Document exception");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

[后端相关截图]

前端代码(Azure Static Web App)

[前端相关截图1]
[前端相关截图2]

PDF生成相关方法

generatePdf(): void {
    this.serviceEmployee.getPdf().subscribe((response) => {
        const blob = new Blob([response.body], {type: 'application/pdf'});
        const url = window.URL.createObjectURL(blob);
        window.open(url);
    });
}

getPdf(): Observable<any> {
    const route = ROUTES.BACKEND_ROUTE + '/employee/cv/pdf';
    const headers = new HttpHeaders({
        'Content-Type': 'application/pdf',
        Accept: 'application/pdf',
    });
    return this.http.get(route, {
        headers: headers,
        observe: 'response',
        responseType: 'blob',
        withCredentials: true,
    });
}

staticwebapp.config.json配置

{
    "navigationFallback": {
        "rewrite": "/index.html"
    },
    "mimeTypes": {
        ".pdf": "application/pdf"
    }
}

已尝试的解决方法

  • 在staticwebapp.config.json中添加了PDF的MIME类型配置,但没有效果
  • 执行git reset --hard回退到之前确认能正常生成PDF的提交,问题依然存在(推测是因为staticwebapp.config.json的配置依然在生效,比如之前添加的刷新配置)

[尝试操作相关截图1]
[尝试操作相关截图2]

备注:内容来源于stack exchange,提问作者Mateusz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 07:03:03