-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone.go
More file actions
53 lines (49 loc) · 1.49 KB
/
clone.go
File metadata and controls
53 lines (49 loc) · 1.49 KB
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
package gopdf
// Clone creates a deep copy of the GoPdf instance by serializing and
// re-importing the document. The cloned document is independent — changes
// to the clone do not affect the original, and vice versa.
//
// This is useful for creating variants of a document (e.g. different
// watermarks or stamps) without re-generating from scratch.
//
// Note: Header/footer callback functions are NOT cloned (they are set to nil).
// Font data, images, and all page content are fully duplicated.
//
// Example:
//
// original := &gopdf.GoPdf{}
// original.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
// original.AddPage()
// // ... build document ...
//
// clone, err := original.Clone()
// if err != nil { log.Fatal(err) }
// // clone is a fully independent copy
// clone.SetPage(1)
// clone.SetXY(100, 100)
// clone.Text("Only in clone")
// clone.WritePdf("clone.pdf")
func (gp *GoPdf) Clone() (*GoPdf, error) {
// Serialize the current document to bytes.
data, err := gp.GetBytesPdfReturnErr()
if err != nil {
return nil, err
}
// Re-import into a new GoPdf instance.
clone := &GoPdf{}
err = clone.OpenPDFFromBytes(data, nil)
if err != nil {
return nil, err
}
// Copy metadata that isn't part of the PDF stream.
clone.pdfVersion = gp.pdfVersion
if gp.xmpMetadata != nil {
metaCopy := *gp.xmpMetadata
clone.xmpMetadata = &metaCopy
}
if len(gp.pageLabels) > 0 {
clone.pageLabels = make([]PageLabel, len(gp.pageLabels))
copy(clone.pageLabels, gp.pageLabels)
}
return clone, nil
}