@@ -121,17 +121,14 @@ def save_pdf(source, vector=True, prog_cb=lambda x: None):
121
121
# ...
122
122
123
123
# Generate page information
124
- contentdict = {}
125
- if source .exists ('{ID}.content' ):
126
- with source .open ('{ID}.content' , 'r' ) as f :
127
- contentdict = json .load (f )
128
124
# If a PDF file was uploaded, but never opened, there may not be
129
125
# a .content file. So, just load a barebones one with a 'pages'
130
126
# key of zero length, so it doesn't break the rest of the
131
- # process. This is here, instead of in __init__, because it is
132
- # more explainable/straightforward here.
133
- if not 'pages' in contentdict :
134
- contentdict ['pages' ] = []
127
+ # process.
128
+ pages = []
129
+ if source .exists ('{ID}.content' ):
130
+ with source .open ('{ID}.content' , 'r' ) as f :
131
+ pages = json .load (f ).get ('pages' , [])
135
132
136
133
# Render each page as a pdf
137
134
tmpfh = tempfile .TemporaryFile ()
@@ -143,13 +140,13 @@ def save_pdf(source, vector=True, prog_cb=lambda x: None):
143
140
# iteration so they get released by garbage collector.
144
141
changed_pages = []
145
142
annotations = []
146
- for i in range (0 , len (contentdict [ ' pages' ] )):
147
- page = DocumentPage (source , contentdict , i )
143
+ for i in range (0 , len (pages )):
144
+ page = DocumentPage (source , pages [ i ] , i )
148
145
if source .exists (page .rmpath ):
149
146
changed_pages .append (i )
150
147
page .render_to_painter (pdf_canvas , vector )
151
148
annotations .append (page .get_grouped_annotations ())
152
- prog_cb ((i + 1 ) / len (contentdict [ ' pages' ] ) * 50 )
149
+ prog_cb ((i + 1 ) / len (pages ) * 50 )
153
150
pdf_canvas .save ()
154
151
tmpfh .seek (0 )
155
152
@@ -603,18 +600,17 @@ def merge_pages(basepage, rmpage, changed_page):
603
600
604
601
class DocumentPage :
605
602
# A single page in a document
606
- def __init__ (self ,
607
- source ,
608
- doc_contentdict , # Added
609
- pagenum ):
603
+ def __init__ (self , source , pid , pagenum ):
610
604
# Page 0 is the first page!
611
605
self .source = source
612
606
self .num = pagenum
613
607
614
- # get page id
615
- pid = str (pagenum ) #For download; from device: doc_contentdict['pages'][pagenum]
616
-
608
+ # On disk, these files are named by a UUID
617
609
self .rmpath = f'{{ID}}/{ pid } .rm'
610
+ if not source .exists (self .rmpath ):
611
+ # From the API, these files are just numbered
612
+ pid = str (pagenum )
613
+ self .rmpath = f'{{ID}}/{ pid } .rm'
618
614
619
615
# Try to load page metadata
620
616
self .metadict = None
@@ -625,23 +621,21 @@ def __init__(self,
625
621
626
622
# Try to load template
627
623
self .template = None
628
- tmpnamearray = []
624
+ template_names = []
629
625
pagedatapath = '{ID}.pagedata'
630
626
if source .exists (pagedatapath ):
631
627
with source .open (pagedatapath , 'r' ) as f :
632
- lines = f .read ()
633
- for line in lines .splitlines ():
634
- tmpnamearray .append (line )
628
+ template_names = f .read ().splitlines ()
635
629
636
- if tmpnamearray :
630
+ if template_names :
637
631
# I have encountered an issue with some PDF files, where the
638
632
# rM won't save the page template for later pages. In this
639
633
# case, just take the last-available page template, which
640
634
# is usually 'Blank'.
641
- tmpname = tmpnamearray [max (self .num , len (tmpnamearray ) - 1 )]
642
- tmparchivepath = TEMPLATE_PATH / f'{ tmpname } .svg'
643
- if tmpname != 'Blank' and tmparchivepath .exists ():
644
- self .template = str (tmparchivepath )
635
+ template_name = template_names [max (self .num , len (template_names ) - 1 )]
636
+ template_path = TEMPLATE_PATH / f'{ template_name } .svg'
637
+ if template_name != 'Blank' and template_path .exists ():
638
+ self .template = str (template_path )
645
639
646
640
# Load layers
647
641
self .layers = []
@@ -877,3 +871,11 @@ def write_to_output(stream, fn):
877
871
save_pdf (ZipSource (zf ), vector , print ),
878
872
f'testing/output-zip-{ format_ } .pdf'
879
873
)
874
+ # On disk
875
+ write_to_output (
876
+ save_pdf (
877
+ FSSource ('testing/ondisk' , 'cb736ad2-b869-4253-979a-dbc5c01a9000' ),
878
+ vector , print
879
+ ),
880
+ f'testing/output-disk-{ format_ } .pdf'
881
+ )
0 commit comments