Okay, after a lot of Googling and fiddling around, I have a python script that will take a Sailwave HTML file as an argument, convert it to grayscale PDF and send it to an email address.
In my situation, I’m running this on a Raspberry PI that has an FTP server on it, so it does all the work. I wanted to do this on my hosting provider, but they don’t have the necessary programs (WKHTMLTOPDF, mainly) to make it work. That’s fine, I’ll figure out a way to further send the html file to a web server for public consumption if that becomes necessary.
In my case, I’m using the Linux INOTIFY command to run in the background and detect when a new file has arrived in a directory, and then pass that filename to this script. I still have some work to do on that, as I can’t seem to get the CREATE and MODIFY options of INOTIFY to work simultaneously.
If you wish to use this with GMAIL, you need to reduce the security on your gmail account to allow operation with apps that have less security.
Thanks to Jon for getting me going with PDFKIT, and to others for their ideas and encouragement. Apologies in advance for my poor programming style - this is literally my first Python script.
#!/usr/bin/env python
import pdfkit
import smtplib
import sys
import os
import time
from time import gmtime, localtime, strftime
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
print ‘Script activated’
msg = MIMEMultipart()
the fromaddr variable is also used for sign-on to gmail, so it needs to be the address used there
fromaddr = ‘’
toaddr = ‘’
pdfDir = ‘’
timeString = strftime("%d %b %Y %H:%M:%S", localtime())
msg[‘From’] = fromaddr
msg[‘To’] = toaddr
msg[‘Subject’] = 'Sailwave Results in PDF Format ’ + timeString
body = ‘Here are the latest results in PDF format.’
msg.attach(MIMEText(body, ‘plain’))
timeNow = strftime("%d%m%y%H%M%S", localtime())
base = os.path.basename(str(sys.argv[1]))
base = os.path.splitext(base)[0]
#WKHTMLTOPDF will not overwrite a file, so you need to use a different filename
#This just adds the time to the name to make it unique
pdf_filename = pdfDir + base + timeNow +’.pdf’
options = {
‘page-size’: ‘Letter’,
‘margin-top’: ‘0.75in’,
‘margin-right’: ‘0.75in’,
‘margin-bottom’: ‘0.75in’,
‘margin-left’: ‘0.75in’,
‘encoding’: “UTF-8”,
‘grayscale’: ‘’,
‘orientation’: ‘Landscape’,
‘load-error-handling’: ‘ignore’
}
mail_filename = base + timeNow + ‘.pdf’
print ‘Got to pdf conversion’
pdfkit.from_url(str(sys.argv[1]), pdf_filename, options=options)
attachment = open(pdf_filename, “rb”)
part = MIMEBase(‘application’, ‘octet-stream’)
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header(‘Content-Disposition’, “attachment; filename= %s” % mail_filename)
msg.attach(part)
print ‘Ready to attempt email’
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(fromaddr, "<your gmail password>")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
print 'Email successful'
except:
print 'Something went wrong with mail...'