Automatic PDF Conversion [1 Attachment]

Thanks for that. I’d be very interested in having a look at your python script!

I think conversion to PDF would be a great feature to have, and having some control over the output format would also help a lot. The desires of IODA aside, I think we all see instances where Sailwave output ultimately gets converted to PDF for publishing or even printing, odd as that seems. The ability to ultimately email PDF would provide a great means of preserving output format, which is often a challenge with HTML.

Andy

···

Hi Andy,

I’m away for the weekend but it is only a few lines of python code

It uses pdfkit

so you need

import pdfkit

pdfkit.from_url(‘xyz’, ‘out.pdf’)

the script picks up the first parameter in the command that is passed to it and puts in place of xyz This would give you a file converted to out.pdf

The script I did also striped the file name and replaced the out with the name of the input file.

If you can wait till next week I’ll hunt out the script but it was fairly simple

Jon


Mailtrack
Sender notified by

                [Mailtrack](https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality4&)
···

Jon Eskdale
07530 112233

Skype “eskdale”

Hi Andy - Back home

import pdfkit

import sys

import os

base = os.path.basename(str(sys.argv[1]))

base = os.path.splitext(base)[0]

pdfkit.from_url(str(sys.argv[1]), base + ‘.pdf’)

That was the Python code I used to test - This generates a pdf with the same name as the html file but with a PDF name extention.

Jon


Mailtrack
Sender notified by

                [Mailtrack](https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality4&)

Hi Andy,

I’m away for the weekend but it is only a few lines of python code

It uses pdfkit

so you need

import pdfkit

pdfkit.from_url(‘xyz’, ‘out.pdf’)

the script picks up the first parameter in the command that is passed to it and puts in place of xyz This would give you a file converted to out.pdf

The script I did also striped the file name and replaced the out with the name of the input file.

If you can wait till next week I’ll hunt out the script but it was fairly simple

Jon


Mailtrack
Sender notified by

                [Mailtrack](https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality4&)
···

Jon Eskdale
07530 112233

Skype “eskdale”

Jon Eskdale

03333 443377

07530 112233

Jon

I think you sent this reply to wrong Andy

···

Jon Eskdale
07530 112233

Skype “eskdale”

Jon Eskdale

03333 443377

07530 112233

Very cool. Thanks Jon.

—In sailwave@yahoogroups.com, <jon@…> wrote :

Hi Andy - Back home

import pdfkit

import sys

import os

base = os.path.basename(str(sys.argv[1]))

base = os.path.splitext(base)[0]

pdfkit.from_url(str(sys.argv[1]), base + ‘.pdf’)

···

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...'

If you have SSH access at your host you should be able to get it to work
there. The host for my personal webserver literally offers no support
but does allow SSH access and so simply uploading WKHTMLTO to my
webserver allows me to make PDF pages with PHP. Never programmed with
Python but it shouldn't be a problem.

With my webserver (standard Apache installation) you can send the emails
right from the server rather than having to use some external mail
server. I think that is pretty normal and if you can get the script to
run on your webserver that would simply matters slightly (and remove a
source of possible errors/problems).

Art

···

On 6/17/2018 12:53 PM, andy@sailor.nu [sailwave] wrote:

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 = '<from address>'
toaddr = '<address to email to>'
pdfDir = '<path to where you want the PDFs stored>'

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...'

Posted by: andy@sailor.nu

Thanks. I’ve got a static version of the program, but I was concerned about running it on my host. I definitely have SSH access to my server. I’ll have a look. It turns out that the static version I got can be used with PHP, so I may end up re-writing this in PHP.

For now, it works! I’ll have a look at changing it when I have time.

Now off this morning to help unload 150 new Optimist dinghies and semi-truck load of coach RIBs!