Skip to content

Commit 450200d

Browse files
jgoeljgoel
jgoel
authored and
jgoel
committed
Implement bytes interface for handling zip files
1 parent 400b3e2 commit 450200d

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

rm2pdf.go

+34-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rm2pdf
22

33
import (
44
"archive/zip"
5+
"bytes"
56
"io"
67
"io/ioutil"
78
"strings"
@@ -37,20 +38,9 @@ func RenderRmFile(input io.ReadCloser, output io.Writer) error {
3738
return nil
3839
}
3940

40-
// RenderRmNotebook converts an entire Remarkable Notebook
41-
// and renders it as a multiple-page PDF.
42-
//
43-
// input is the name of the Notebook, in zip, format, to open.
44-
//
45-
// output is where the PDF is written.
46-
func RenderRmNotebook(input string, output io.Writer) error {
47-
reader, err := zip.OpenReader(input)
48-
defer reader.Close()
49-
50-
if err != nil {
51-
return err
52-
}
53-
41+
// RenderRmNotebookFromBytes is identical to RenderRmNotebook, but
42+
// takes a *zip.Reader as input.
43+
func RenderRmNotebookFromZip(reader *zip.Reader, output io.Writer) error {
5444
pdf := gofpdf.New("P", "in", "letter", "")
5545

5646
for _, file := range reader.File {
@@ -85,3 +75,33 @@ func RenderRmNotebook(input string, output io.Writer) error {
8575

8676
return nil
8777
}
78+
79+
// RenderRmNotebookFromBytes is identical to RenderRmNotebook, but
80+
// takes a byte array of the zip file as input.
81+
func RenderRmNotebookFromBytes(input []byte, output io.Writer) error {
82+
inputReader := bytes.NewReader(input)
83+
reader, err := zip.NewReader(inputReader, inputReader.Size())
84+
85+
if err != nil {
86+
return err
87+
}
88+
89+
return RenderRmNotebookFromZip(reader, output)
90+
}
91+
92+
// RenderRmNotebook converts an entire Remarkable Notebook
93+
// and renders it as a multiple-page PDF.
94+
//
95+
// input is the name of the Notebook, in zip, format, to open.
96+
//
97+
// output is where the PDF is written.
98+
func RenderRmNotebook(input string, output io.Writer) error {
99+
reader, err := zip.OpenReader(input)
100+
defer reader.Close()
101+
102+
if err != nil {
103+
return err
104+
}
105+
106+
return RenderRmNotebookFromZip(&reader.Reader, output)
107+
}

0 commit comments

Comments
 (0)