Google App Engine 转换 API 因 BackendError 失败

发布于 2024-12-27 23:48:22 字数 1156 浏览 3 评论 0原文

我正在尝试将 html 转换为 pdf
如果我不包含任何图像,转换工作正常,但如果包含图像,转换就会失败,并显示错误代码 3 和说明 BackendError
我指的是我的 html 资产中包含为 static/thumb.jpg 的图像资产。

def prepare_bar_attachment(bars):
    asset = conversion.Asset('text/html',
                             render_template('bar/print.html',
                                             bars=bars),
                             'print.html')
    thumbnail = None
    if bar.thumbnailurl:
        img_response = urlfetch.fetch(bar.thumbnailurl)
        if img_response.status_code == 200:
            thumbnail = conversion.Asset('image/jpeg', img_response.content,
                                         'thumb.jpg')
    conv = conversion.Conversion(asset, 'application/pdf')
    if thumbnail:
        conv.add_asset(thumbnail)
    result = conversion.convert(conv)
    if result.assets:
        attachment = [('Bars.pdf', result.assets[0].data)]
    else:
        attachment = []
        app.logger.error('Error Code: %s\nDescription\%s'%\
                             (result.error_code, result.error_text))
    return attachment

I am trying to convert html to pdf.
The conversion works fine if I don't include any images, but if I include images it fails with error code 3 and Description BackendError.
I'm referring the image asset included as static/thumb.jpg in my html asset.

def prepare_bar_attachment(bars):
    asset = conversion.Asset('text/html',
                             render_template('bar/print.html',
                                             bars=bars),
                             'print.html')
    thumbnail = None
    if bar.thumbnailurl:
        img_response = urlfetch.fetch(bar.thumbnailurl)
        if img_response.status_code == 200:
            thumbnail = conversion.Asset('image/jpeg', img_response.content,
                                         'thumb.jpg')
    conv = conversion.Conversion(asset, 'application/pdf')
    if thumbnail:
        conv.add_asset(thumbnail)
    result = conversion.convert(conv)
    if result.assets:
        attachment = [('Bars.pdf', result.assets[0].data)]
    else:
        attachment = []
        app.logger.error('Error Code: %s\nDescription\%s'%\
                             (result.error_code, result.error_text))
    return attachment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2025-01-03 23:48:22

这可能是因为您已将项目映射为静态您的应用程序代码无法访问 app.yaml 中的资产。尝试将图像包含在代码中的某个位置,或者不在 app.yaml 中将图像映射为静态。

听起来这是因为 html 资源中的 img src 路径应该与资源路径匹配。

This is probably because items you've mapped as static assets in your app.yaml can't be accessed by your app's code. Try either including the image somewhere within your code, or without mapping the images as static in app.yaml.

It sounds like this was because the img src path in the html asset should match the asset path.

静若繁花 2025-01-03 23:48:22

就我而言,当我引用未作为资产提供的图像时,会生成 BackendError。

奇怪的是,当 CSS 引用图像,但 CSS 规则不适用时,它工作正常。

当 HTML 更改并且之前未使用的 CSS 规则(引用丢失的图像资源)被应用到新的/更改的 HTML 元素时,错误开始出现。

因此,只要这些 CSS 规则本身不被使用,就可以在 CSS 中引用缺失的图像资源。

In my case BackendError was generated when I referenced an image that was not provided as an asset.

The curious thing was that when an image was referenced by a CSS, but the CSS rule did not apply it worked OK.

The error started showing up when HTML changed and and the previously unused CSS rule — referencing a missing image asset — got applied to the new/changed HTML element.

So it is OK to reference missing image assets in CSS as long as these CSS rules are not used themselves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文