pixelcog / gmail-to-pdf

A Google Apps Script library for converting Gmail messages to PDFs for easy archival.
http://bit.ly/1CsBl8U
122 stars 38 forks source link

Attachments not showing inline #17

Open tjrexer opened 3 years ago

tjrexer commented 3 years ago

I've been trying to get the attachments to show inline, but they seem to all be added at the end. Is this a problem with .getAs? All of the images are rendered correctly, but the Blob class just seems to ignore all the cid references.

I haven't seen any traction on this repo in a while, so I don't expect an answer from pixelcog.

steinkem commented 3 years ago

I had the same issue, fixed by adding code below to the function embedInlineImages_. Add it right before the line return processImgAttachments(html);

html = html.replace(/(<img[^>]+src=)(["'])((?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, src) { var idx = images.findIndex(item => item.getName() === src.replace("cid:","")); return tag + q + (renderDataUri_(images[idx]) || images[idx]) + q; });

bleu34 commented 2 years ago

I had the same issue, fixed by adding code below to the function embedInlineImages_. Add it right before the line return processImgAttachments(html);

html = html.replace(/(<img[^>]+src=)(["'])((?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, src) { var idx = images.findIndex(item => item.getName() === src.replace("cid:","")); return tag + q + (renderDataUri_(images[idx]) || images[idx]) + q; });

Thanks a lot @steinkem, the trick is putting the images inline ! I have a bug with some mails which are not or partially putting the inline images. I was not able to determine what triggers this bug. Did you also experienced that ? and did you solved it ?

bleu34 commented 2 years ago
I made a test with 1 mail containing 2 images, I then replied with 1 image, replied with 1 image, replied with 1 image, replied with 1 image. And the bug is really ramdom : Mail 1 Mail 2 Mail 3 Mail 4 Mail 5
Image 6 ------- ------- ------- ------- OK
Image 5 ------- ------- ------- Fail Fail
Image 4 ------- ------- OK Fail Fail
Image 3 ------- Fail OK OK OK
Image 2 Fail OK OK Fail OK
Image 1 OK Fail Fail OK OK

I launched several times the script for the same mail, it is each time the same Fail.

bleu34 commented 2 years ago

I now guess it is due to the Blob size limitation of 2Mo of the Replace function.

AlexBeep commented 1 year ago

You can see the problem when clicking on "original message" in gmail. In the area containing img-cids there are newline or carrige return divided by "=".

If this breaks the tag (e.g. <img= src=3D"cid:ii_l9yhg5hk0" ) there will nothing be found.

I added this code in the beginning of function embedInlineImages_

let rawnew = raw.replace(/=[\n\r]/gi, '');
rawnew = rawnew.replace(/[\n\r]/gi, '');

and changed the line raw.replace(/<img[^>]+src=(?:3D)?(["'])cid:((?:(?!\1)[^\\]|\\.)*)\1/gi, function(m, q, cid) { to rawnew.replace(/<img[^>]+src=(?:3D)?(["'])cid:((?:(?!\1)[^\\]|\\.)*)\1/gi, function(m, q, cid) {

This regex can probably be written in one line but my brain is not processing that :-)

bleu34 commented 1 year ago

@AlexBeep : Well spotted for the area divided by "=". Your Regex does the job.

I have another problem with images which are not inserted in the mail with img-cids, but which are hyperlinks like this : </div><div><sp= an id=3D"gmail-docs-internal-guid-xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"><im= g width=3D"960px;" height=3D"468px;" src=3D"https://lh4.googleusercontent.c= om/xxxxxxxyyyyyyyyyyyzzzzzzzzzzxxxxxxxxyyyyyyyyyyyyyyyzzzzzzzxxxxxxxyyyyyy= xxxxxxxyyyyyyyyyyyzzzzzzzzzzxxxxxxxxyyyyyyyyyyyyyyyzzzzzzzxxxxxxxyyyyyyxxxx= yyyyyzz=3Ds2048"> There is the same divider "=" but also the end is not needed '=3Ds2048' (and the = is not a divider).

Have you experienced the same and did somebody found the solution ?