简体   繁体   中英

converting Gmail to PDF: embedded images in HTML

I am using the Gmail API to download e-mails. When these e-mails are HTML, I try to convert them to PDF using Python's pdfkit.

This works in many cases but in some cases the html payload contains image tags like src=“cid:169abdc4ae2c4da871d2” .

It seems that this "cid" tag refers to an image sent as part of the multipart e-mail, but this cannot be processed by PDFkit. Error is:

wkhtmltopdf reported an error:
Loading pages (1/6)
Error: Failed to load cid:169abf0d0cdfffb7aff2, with network status code 301 and http status code 0 - Protocol "cid" is unknown

How can I solve this? Is there a way to convert this HTML I get from the gmail payload to standard HTML with proper picture sources?

You can use "remove_tags" method in the w3lib Package :

Remove all tags:

import w3lib.html
doc = '<div><p><b>This is a link:</b> <a href="http://www.example.com">example</a></p></div>'
w3lib.html.remove_tags(doc)
'This is a link: example'

Remove specific tags:

 w3lib.html.remove_tags(doc, which_ones=('a','b'))
'<div><p>This is a link: example</p></div>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM