1
16
17 package gofpdf
18
19 import (
20 "bytes"
21 "crypto/sha1"
22 "encoding/gob"
23 "encoding/json"
24 "fmt"
25 "io"
26 "time"
27 )
28
29
30 const (
31 cnFpdfVersion = "1.7"
32 )
33
34 type blendModeType struct {
35 strokeStr, fillStr, modeStr string
36 objNum int
37 }
38
39 type gradientType struct {
40 tp int
41 clr1Str, clr2Str string
42 x1, y1, x2, y2, r float64
43 objNum int
44 }
45
46 const (
47
48 OrientationPortrait = "portrait"
49
50
51 OrientationLandscape = "landscape"
52 )
53
54 const (
55
56 UnitPoint = "pt"
57
58 UnitMillimeter = "mm"
59
60 UnitCentimeter = "cm"
61
62 UnitInch = "inch"
63 )
64
65 const (
66
67 PageSizeA3 = "A3"
68
69 PageSizeA4 = "A4"
70
71 PageSizeA5 = "A5"
72
73 PageSizeLetter = "Letter"
74
75 PageSizeLegal = "Legal"
76 )
77
78 const (
79
80 BorderNone = ""
81
82 BorderFull = "1"
83
84 BorderLeft = "L"
85
86 BorderTop = "T"
87
88 BorderRight = "R"
89
90 BorderBottom = "B"
91 )
92
93 const (
94
95 LineBreakNone = 0
96
97 LineBreakNormal = 1
98
99 LineBreakBelow = 2
100 )
101
102 const (
103
104 AlignLeft = "L"
105
106 AlignRight = "R"
107
108 AlignCenter = "C"
109
110 AlignTop = "T"
111
112 AlignBottom = "B"
113
114 AlignMiddle = "M"
115
116 AlignBaseline = "B"
117 )
118
119 type colorMode int
120
121 const (
122 colorModeRGB colorMode = iota
123 colorModeSpot
124 colorModeCMYK
125 )
126
127 type colorType struct {
128 r, g, b float64
129 ir, ig, ib int
130 mode colorMode
131 spotStr string
132 gray bool
133 str string
134 }
135
136
137 type spotColorType struct {
138 id, objID int
139 val cmykColorType
140 }
141
142
143 type cmykColorType struct {
144 c, m, y, k byte
145 }
146
147
148
149 type SizeType struct {
150 Wd, Ht float64
151 }
152
153
154
155 type PointType struct {
156 X, Y float64
157 }
158
159
160 func (p PointType) XY() (float64, float64) {
161 return p.X, p.Y
162 }
163
164
165
166
167 type ImageInfoType struct {
168 data []byte
169 smask []byte
170 n int
171 w float64
172 h float64
173 cs string
174 pal []byte
175 bpc int
176 f string
177 dp string
178 trns []int
179 scale float64
180 dpi float64
181 i string
182 }
183
184 func generateImageID(info *ImageInfoType) (string, error) {
185 b, err := info.GobEncode()
186 return fmt.Sprintf("%x", sha1.Sum(b)), err
187 }
188
189
190 func (info *ImageInfoType) GobEncode() (buf []byte, err error) {
191 fields := []interface{}{info.data, info.smask, info.n, info.w, info.h, info.cs,
192 info.pal, info.bpc, info.f, info.dp, info.trns, info.scale, info.dpi}
193 w := new(bytes.Buffer)
194 encoder := gob.NewEncoder(w)
195 for j := 0; j < len(fields) && err == nil; j++ {
196 err = encoder.Encode(fields[j])
197 }
198 if err == nil {
199 buf = w.Bytes()
200 }
201 return
202 }
203
204
205
206 func (info *ImageInfoType) GobDecode(buf []byte) (err error) {
207 fields := []interface{}{&info.data, &info.smask, &info.n, &info.w, &info.h,
208 &info.cs, &info.pal, &info.bpc, &info.f, &info.dp, &info.trns, &info.scale, &info.dpi}
209 r := bytes.NewBuffer(buf)
210 decoder := gob.NewDecoder(r)
211 for j := 0; j < len(fields) && err == nil; j++ {
212 err = decoder.Decode(fields[j])
213 }
214
215 info.i, err = generateImageID(info)
216 return
217 }
218
219
220
221
222
223 func (f *Fpdf) PointConvert(pt float64) (u float64) {
224 return pt / f.k
225 }
226
227
228 func (f *Fpdf) PointToUnitConvert(pt float64) (u float64) {
229 return pt / f.k
230 }
231
232
233
234
235
236 func (f *Fpdf) UnitToPointConvert(u float64) (pt float64) {
237 return u * f.k
238 }
239
240
241
242 func (info *ImageInfoType) Extent() (wd, ht float64) {
243 return info.Width(), info.Height()
244 }
245
246
247 func (info *ImageInfoType) Width() float64 {
248 return info.w / (info.scale * info.dpi / 72)
249 }
250
251
252 func (info *ImageInfoType) Height() float64 {
253 return info.h / (info.scale * info.dpi / 72)
254 }
255
256
257
258
259
260 func (info *ImageInfoType) SetDpi(dpi float64) {
261 info.dpi = dpi
262 }
263
264 type fontFileType struct {
265 length1, length2 int64
266 n int
267 embedded bool
268 content []byte
269 fontType string
270 }
271
272 type linkType struct {
273 x, y, wd, ht float64
274 link int
275 linkStr string
276 }
277
278 type intLinkType struct {
279 page int
280 y float64
281 }
282
283
284 type outlineType struct {
285 text string
286 level, parent, first, last, next, prev int
287 y float64
288 p int
289 }
290
291
292
293
294
295
296 type InitType struct {
297 OrientationStr string
298 UnitStr string
299 SizeStr string
300 Size SizeType
301 FontDirStr string
302 }
303
304
305
306
307
308
309 type FontLoader interface {
310 Open(name string) (io.Reader, error)
311 }
312
313
314
315 type Pdf interface {
316 AddFont(familyStr, styleStr, fileStr string)
317 AddFontFromBytes(familyStr, styleStr string, jsonFileBytes, zFileBytes []byte)
318 AddFontFromReader(familyStr, styleStr string, r io.Reader)
319 AddLayer(name string, visible bool) (layerID int)
320 AddLink() int
321 AddPage()
322 AddPageFormat(orientationStr string, size SizeType)
323 AddSpotColor(nameStr string, c, m, y, k byte)
324 AliasNbPages(aliasStr string)
325 ArcTo(x, y, rx, ry, degRotate, degStart, degEnd float64)
326 Arc(x, y, rx, ry, degRotate, degStart, degEnd float64, styleStr string)
327 BeginLayer(id int)
328 Beziergon(points []PointType, styleStr string)
329 Bookmark(txtStr string, level int, y float64)
330 CellFormat(w, h float64, txtStr, borderStr string, ln int, alignStr string, fill bool, link int, linkStr string)
331 Cellf(w, h float64, fmtStr string, args ...interface{})
332 Cell(w, h float64, txtStr string)
333 Circle(x, y, r float64, styleStr string)
334 ClearError()
335 ClipCircle(x, y, r float64, outline bool)
336 ClipEllipse(x, y, rx, ry float64, outline bool)
337 ClipEnd()
338 ClipPolygon(points []PointType, outline bool)
339 ClipRect(x, y, w, h float64, outline bool)
340 ClipRoundedRect(x, y, w, h, r float64, outline bool)
341 ClipText(x, y float64, txtStr string, outline bool)
342 Close()
343 ClosePath()
344 CreateTemplateCustom(corner PointType, size SizeType, fn func(*Tpl)) Template
345 CreateTemplate(fn func(*Tpl)) Template
346 CurveBezierCubicTo(cx0, cy0, cx1, cy1, x, y float64)
347 CurveBezierCubic(x0, y0, cx0, cy0, cx1, cy1, x1, y1 float64, styleStr string)
348 CurveCubic(x0, y0, cx0, cy0, x1, y1, cx1, cy1 float64, styleStr string)
349 CurveTo(cx, cy, x, y float64)
350 Curve(x0, y0, cx, cy, x1, y1 float64, styleStr string)
351 DrawPath(styleStr string)
352 Ellipse(x, y, rx, ry, degRotate float64, styleStr string)
353 EndLayer()
354 Err() bool
355 Error() error
356 GetAlpha() (alpha float64, blendModeStr string)
357 GetAutoPageBreak() (auto bool, margin float64)
358 GetCellMargin() float64
359 GetConversionRatio() float64
360 GetDrawColor() (int, int, int)
361 GetDrawSpotColor() (name string, c, m, y, k byte)
362 GetFillColor() (int, int, int)
363 GetFillSpotColor() (name string, c, m, y, k byte)
364 GetFontDesc(familyStr, styleStr string) FontDescType
365 GetFontSize() (ptSize, unitSize float64)
366 GetImageInfo(imageStr string) (info *ImageInfoType)
367 GetLineWidth() float64
368 GetMargins() (left, top, right, bottom float64)
369 GetPageSizeStr(sizeStr string) (size SizeType)
370 GetPageSize() (width, height float64)
371 GetStringWidth(s string) float64
372 GetTextColor() (int, int, int)
373 GetTextSpotColor() (name string, c, m, y, k byte)
374 GetX() float64
375 GetXY() (float64, float64)
376 GetY() float64
377 HTMLBasicNew() (html HTMLBasicType)
378 Image(imageNameStr string, x, y, w, h float64, flow bool, tp string, link int, linkStr string)
379 ImageOptions(imageNameStr string, x, y, w, h float64, flow bool, options ImageOptions, link int, linkStr string)
380 ImageTypeFromMime(mimeStr string) (tp string)
381 LinearGradient(x, y, w, h float64, r1, g1, b1, r2, g2, b2 int, x1, y1, x2, y2 float64)
382 LineTo(x, y float64)
383 Line(x1, y1, x2, y2 float64)
384 LinkString(x, y, w, h float64, linkStr string)
385 Link(x, y, w, h float64, link int)
386 Ln(h float64)
387 MoveTo(x, y float64)
388 MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill bool)
389 Ok() bool
390 OpenLayerPane()
391 OutputAndClose(w io.WriteCloser) error
392 OutputFileAndClose(fileStr string) error
393 Output(w io.Writer) error
394 PageCount() int
395 PageNo() int
396 PageSize(pageNum int) (wd, ht float64, unitStr string)
397 PointConvert(pt float64) (u float64)
398 PointToUnitConvert(pt float64) (u float64)
399 Polygon(points []PointType, styleStr string)
400 RadialGradient(x, y, w, h float64, r1, g1, b1, r2, g2, b2 int, x1, y1, x2, y2, r float64)
401 RawWriteBuf(r io.Reader)
402 RawWriteStr(str string)
403 Rect(x, y, w, h float64, styleStr string)
404 RegisterAlias(alias, replacement string)
405 RegisterImage(fileStr, tp string) (info *ImageInfoType)
406 RegisterImageOptions(fileStr string, options ImageOptions) (info *ImageInfoType)
407 RegisterImageOptionsReader(imgName string, options ImageOptions, r io.Reader) (info *ImageInfoType)
408 RegisterImageReader(imgName, tp string, r io.Reader) (info *ImageInfoType)
409 SetAcceptPageBreakFunc(fnc func() bool)
410 SetAlpha(alpha float64, blendModeStr string)
411 SetAuthor(authorStr string, isUTF8 bool)
412 SetAutoPageBreak(auto bool, margin float64)
413 SetCatalogSort(flag bool)
414 SetCellMargin(margin float64)
415 SetCompression(compress bool)
416 SetCreationDate(tm time.Time)
417 SetCreator(creatorStr string, isUTF8 bool)
418 SetDashPattern(dashArray []float64, dashPhase float64)
419 SetDisplayMode(zoomStr, layoutStr string)
420 SetDrawColor(r, g, b int)
421 SetDrawSpotColor(nameStr string, tint byte)
422 SetError(err error)
423 SetErrorf(fmtStr string, args ...interface{})
424 SetFillColor(r, g, b int)
425 SetFillSpotColor(nameStr string, tint byte)
426 SetFont(familyStr, styleStr string, size float64)
427 SetFontLoader(loader FontLoader)
428 SetFontLocation(fontDirStr string)
429 SetFontSize(size float64)
430 SetFontStyle(styleStr string)
431 SetFontUnitSize(size float64)
432 SetFooterFunc(fnc func())
433 SetFooterFuncLpi(fnc func(lastPage bool))
434 SetHeaderFunc(fnc func())
435 SetHeaderFuncMode(fnc func(), homeMode bool)
436 SetHomeXY()
437 SetJavascript(script string)
438 SetKeywords(keywordsStr string, isUTF8 bool)
439 SetLeftMargin(margin float64)
440 SetLineCapStyle(styleStr string)
441 SetLineJoinStyle(styleStr string)
442 SetLineWidth(width float64)
443 SetLink(link int, y float64, page int)
444 SetMargins(left, top, right float64)
445 SetPageBoxRec(t string, pb PageBox)
446 SetPageBox(t string, x, y, wd, ht float64)
447 SetPage(pageNum int)
448 SetProtection(actionFlag byte, userPassStr, ownerPassStr string)
449 SetRightMargin(margin float64)
450 SetSubject(subjectStr string, isUTF8 bool)
451 SetTextColor(r, g, b int)
452 SetTextSpotColor(nameStr string, tint byte)
453 SetTitle(titleStr string, isUTF8 bool)
454 SetTopMargin(margin float64)
455 SetUnderlineThickness(thickness float64)
456 SetXmpMetadata(xmpStream []byte)
457 SetX(x float64)
458 SetXY(x, y float64)
459 SetY(y float64)
460 SplitLines(txt []byte, w float64) [][]byte
461 String() string
462 SVGBasicWrite(sb *SVGBasicType, scale float64)
463 Text(x, y float64, txtStr string)
464 TransformBegin()
465 TransformEnd()
466 TransformMirrorHorizontal(x float64)
467 TransformMirrorLine(angle, x, y float64)
468 TransformMirrorPoint(x, y float64)
469 TransformMirrorVertical(y float64)
470 TransformRotate(angle, x, y float64)
471 TransformScale(scaleWd, scaleHt, x, y float64)
472 TransformScaleX(scaleWd, x, y float64)
473 TransformScaleXY(s, x, y float64)
474 TransformScaleY(scaleHt, x, y float64)
475 TransformSkew(angleX, angleY, x, y float64)
476 TransformSkewX(angleX, x, y float64)
477 TransformSkewY(angleY, x, y float64)
478 Transform(tm TransformMatrix)
479 TransformTranslate(tx, ty float64)
480 TransformTranslateX(tx float64)
481 TransformTranslateY(ty float64)
482 UnicodeTranslatorFromDescriptor(cpStr string) (rep func(string) string)
483 UnitToPointConvert(u float64) (pt float64)
484 UseTemplateScaled(t Template, corner PointType, size SizeType)
485 UseTemplate(t Template)
486 WriteAligned(width, lineHeight float64, textStr, alignStr string)
487 Writef(h float64, fmtStr string, args ...interface{})
488 Write(h float64, txtStr string)
489 WriteLinkID(h float64, displayStr string, linkID int)
490 WriteLinkString(h float64, displayStr, targetStr string)
491 }
492
493
494 type PageBox struct {
495 SizeType
496 PointType
497 }
498
499
500 type Fpdf struct {
501 isCurrentUTF8 bool
502 isRTL bool
503 page int
504 n int
505 offsets []int
506 templates map[string]Template
507 templateObjects map[string]int
508 importedObjs map[string][]byte
509 importedObjPos map[string]map[int]string
510 importedTplObjs map[string]string
511 importedTplIDs map[string]int
512 buffer fmtBuffer
513 pages []*bytes.Buffer
514 state int
515 compress bool
516 k float64
517 defOrientation string
518 curOrientation string
519 stdPageSizes map[string]SizeType
520 defPageSize SizeType
521 defPageBoxes map[string]PageBox
522 curPageSize SizeType
523 pageSizes map[int]SizeType
524 pageBoxes map[int]map[string]PageBox
525 unitStr string
526 wPt, hPt float64
527 w, h float64
528 lMargin float64
529 tMargin float64
530 rMargin float64
531 bMargin float64
532 cMargin float64
533 x, y float64
534 lasth float64
535 lineWidth float64
536 fontpath string
537 fontLoader FontLoader
538 coreFonts map[string]bool
539 fonts map[string]fontDefType
540 fontFiles map[string]fontFileType
541 diffs []string
542 fontFamily string
543 fontStyle string
544 underline bool
545 strikeout bool
546 currentFont fontDefType
547 fontSizePt float64
548 fontSize float64
549 ws float64
550 images map[string]*ImageInfoType
551 aliasMap map[string]string
552 pageLinks [][]linkType
553 links []intLinkType
554 attachments []Attachment
555 pageAttachments [][]annotationAttach
556 outlines []outlineType
557 outlineRoot int
558 autoPageBreak bool
559 acceptPageBreak func() bool
560 pageBreakTrigger float64
561 inHeader bool
562 headerFnc func()
563 headerHomeMode bool
564 inFooter bool
565 footerFnc func()
566 footerFncLpi func(bool)
567 zoomMode string
568 layoutMode string
569 xmp []byte
570 producer string
571 title string
572 subject string
573 author string
574 keywords string
575 creator string
576 creationDate time.Time
577 modDate time.Time
578 aliasNbPagesStr string
579 pdfVersion string
580 fontDirStr string
581 capStyle int
582 joinStyle int
583 dashArray []float64
584 dashPhase float64
585 blendList []blendModeType
586 blendMap map[string]int
587 blendMode string
588 alpha float64
589 gradientList []gradientType
590 clipNest int
591 transformNest int
592 err error
593 protect protectType
594 layer layerRecType
595 catalogSort bool
596 nJs int
597 javascript *string
598 colorFlag bool
599 color struct {
600
601 draw, fill, text colorType
602 }
603 spotColorMap map[string]spotColorType
604 userUnderlineThickness float64
605 }
606
607 type encType struct {
608 uv int
609 name string
610 }
611
612 type encListType [256]encType
613
614 type fontBoxType struct {
615 Xmin, Ymin, Xmax, Ymax int
616 }
617
618
619 const (
620
621
622
623 FontFlagFixedPitch = 1 << 0
624
625
626
627 FontFlagSerif = 1 << 1
628
629
630
631 FontFlagSymbolic = 1 << 2
632
633 FontFlagScript = 1 << 3
634
635
636 FontFlagNonsymbolic = 1 << 5
637
638
639 FontFlagItalic = 1 << 6
640
641
642
643 FontFlagAllCap = 1 << 16
644
645
646
647
648
649
650
651 SmallCap = 1 << 18
652
653
654
655
656 ForceBold = 1 << 18
657 )
658
659
660
661
662 type FontDescType struct {
663
664
665
666 Ascent int
667
668
669 Descent int
670
671
672 CapHeight int
673
674
675 Flags int
676
677
678
679
680
681 FontBBox fontBoxType
682
683
684
685
686
687 ItalicAngle int
688
689
690 StemV int
691
692
693
694
695
696 MissingWidth int
697 }
698
699 type fontDefType struct {
700 Tp string
701 Name string
702 Desc FontDescType
703 Up int
704 Ut int
705 Cw []int
706 Enc string
707 Diff string
708 File string
709 Size1, Size2 int
710 OriginalSize int
711 N int
712 DiffN int
713 i string
714 utf8File *utf8FontFile
715 usedRunes map[int]int
716 }
717
718
719 func generateFontID(fdt fontDefType) (string, error) {
720
721 fdt.File = ""
722 b, err := json.Marshal(&fdt)
723 return fmt.Sprintf("%x", sha1.Sum(b)), err
724 }
725
726 type fontInfoType struct {
727 Data []byte
728 File string
729 OriginalSize int
730 FontName string
731 Bold bool
732 IsFixedPitch bool
733 UnderlineThickness int
734 UnderlinePosition int
735 Widths []int
736 Size1, Size2 uint32
737 Desc FontDescType
738 }
739
View as plain text