-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.go
55 lines (51 loc) · 1.48 KB
/
svg.go
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
// Copyright (c) 2009 Helmar Wodtke. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// The MIT License is an OSI approved license and can
// be found at
// http://www.opensource.org/licenses/mit-license.php
// Library to convert PDF pages to SVG.
package svg
import (
"fancy"
"fmt"
"os"
"pdfread"
"strm"
"svgdraw"
"svgtext"
"util"
)
func complain(err string) {
fmt.Printf("%s", err)
os.Exit(1)
}
func Page(pd *pdfread.PdfReaderT, page int) []byte {
pg := pd.Pages()
if page >= len(pg) {
complain("Page does not exist!\n")
}
mbox := util.StringArray(pd.Arr(pd.Att("/MediaBox", pg[page])))
drw := svgdraw.NewTestSvg()
svgtext.New(pd, drw).Page = page
w := strm.Mul(strm.Sub(mbox[2], mbox[0]), "1.25")
h := strm.Mul(strm.Sub(mbox[3], mbox[1]), "1.25")
drw.Write.Out(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"+
"<svg\n"+
" xmlns:svg=\"http://www.w3.org/2000/svg\"\n"+
" xmlns=\"http://www.w3.org/2000/svg\"\n"+
" version=\"1.0\"\n"+
" width=\"%s\"\n"+
" height=\"%s\">\n"+
"<g transform=\"matrix(1.25,0,0,-1.25,%s,%s)\">\n",
w, h,
strm.Mul(mbox[0], "-1.25"),
strm.Mul(mbox[3], "1.25"))
cont := pd.ForcedArray(pd.Dic(pg[page])["/Contents"])
_, ps := pd.DecodedStream(cont[0])
drw.Interpret(fancy.SliceReader(ps))
drw.Draw.CloseDrawing()
drw.Write.Out("</g>\n</svg>\n")
return drw.Write.Content
}