-
Notifications
You must be signed in to change notification settings - Fork 0
/
rM2PDF.py
71 lines (60 loc) · 2.73 KB
/
rM2PDF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# rM2PDF script for the reMarkable reader
# Version: 1.0
# Author: Xin Zhang
#--------------------------------------------------#
# set input_dir and output_dir as same as rMsync.sh
input_dir = '/mnt/d/Github/rM2PDF/original_pdf/'
output_dir = '/mnt/d/Github/rM2PDF/noted_pdf/'
t = '/mnt/d/Github/rM2PDF/temporary/'
#--------------------------------------------------#
import numpy as np
import os,fnmatch
# get names of note_lines
note_line = []
for file in os.listdir(input_dir):
if fnmatch.fnmatch(file,'*.lines'):
note_line = np.append(note_line,file)
# get prefix
prefix = [x[:-6] for x in note_line]
for id,prefix in enumerate (prefix):
# get visible name in metdata
pdf_data = open(input_dir+prefix+'.metadata')
visiable_name = pdf_data.readlines()
visiable_name = visiable_name[10].split(":",1)[1][2:-2].replace(" ", "_")
print visiable_name
# convert .lines to .svg
script = 'python '+'./rM2svg.py -i {line} -o {svg_prefix} -c'
script = script.format (line=input_dir+note_line[id],svg_prefix=t+'temporary')
os.system(script)
# convert .svg to pdf
# os.system('svg2pdf -o '+t+'"%(base)s.pdf" '+t+'*.svg')
os.system('rsvg-convert -f pdf -o '+t+'note.pdf '+t+'*.svg')
os.system('rm '+t+'*.svg')
# check notebook or document
if os.path.isfile(input_dir+prefix+'.pdf'): # document
# combine notes to one pdf
# os.system('convert '+t+'temporary*.pdf '+t+'note.pdf')
# check width and height of original pdf
width_column = "'NR==3{print $4}'"
height_column = "'NR==3{print $5}'"
width = os.popen('./cpdf -page-info '+input_dir+prefix+'.pdf'+' | awk {}'\
.format(width_column)).read()
height = os.popen('./cpdf -page-info '+input_dir+prefix+'.pdf'+' | awk {}'\
.format(height_column)).read()
# check landscape or portrait
if width > height: # landscape
size = "'"+height+' '+width+"'"
scalerotate = './cpdf -scale-to-fit {} '.format(size)+t+'note.pdf '+\
'AND -rotate 90 AND -upright '+t+'note.pdf '+'-o '+t+'note.pdf'
os.system(scalerotate)
else: # portrait
size = "'"+width+' '+height+"'"
scale = './cpdf -scale-to-fit {} '.format(size)+t+'note.pdf '+'-o '+t+'note.pdf'
os.system(scale)
# combine note and original pdf
os.system('./cpdf -combine-pages '+t+'note.pdf '+ input_dir+prefix+'.pdf '\
+'-o '+output_dir+visiable_name+'.pdf')
os.system('rm '+t+'note.pdf')
else: # notebook
os.system('convert '+t+'note.pdf '+output_dir+visiable_name+'.pdf')
os.system('rm '+t+'note.pdf')