-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from unidoc/us-392-file-attachment-examples
[US-392] File attachment examples
- Loading branch information
Showing
7 changed files
with
224 additions
and
1,430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# File Attachments | ||
|
||
Exemplify how to attach files into a PDF document and retrieves it. | ||
|
||
## Examples | ||
|
||
- [pdf_add_attachment.go](pdf_add_attachment.go) Attach files into a PDF document, either by manually loading the file content or passing the file name. | ||
- [pdf_get_attachment.go](pdf_get_attachment.go) Retrieve list of file attachment and save it locally. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<note> | ||
<to>Tove</to> | ||
<from>Jani</from> | ||
<heading>Reminder</heading> | ||
<body>Don't forget me this weekend!</body> | ||
</note> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Attach files into PDF document either by passing the file name or by passing the file content. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gabriel-vasile/mimetype" | ||
"github.com/unidoc/unipdf/v3/common/license" | ||
"github.com/unidoc/unipdf/v3/model" | ||
) | ||
|
||
func init() { | ||
// Make sure to load your metered License API key prior to using the library. | ||
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io | ||
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func main() { | ||
inputPath := "minimal.pdf" | ||
outputPath := "output.pdf" | ||
xmlFile := "dummy.xml" | ||
|
||
err := addAttachment(inputPath, xmlFile, outputPath) | ||
if err != nil { | ||
fmt.Printf("Failed to add attachment: %v", err) | ||
|
||
os.Exit(1) | ||
} | ||
|
||
fmt.Printf("Success, output in %s\n", outputPath) | ||
} | ||
|
||
func addAttachment(inputPath string, attachment string, outputPath string) error { | ||
// Read the input pdf file. | ||
f, err := os.Open(inputPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
pdfReader, err := model.NewPdfReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pdfWriter, err := pdfReader.ToWriter(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
// Instantiate embedded file object from existing file path. | ||
emFile, err := model.NewEmbeddedFile(attachment) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Overwrite attachment name. | ||
emFile.Name = fmt.Sprintf("dummy%d.xml", i+1) | ||
emFile.Description = fmt.Sprintf("Sample Attachment %d", i+1) | ||
emFile.Relationship = model.RelationshipData | ||
|
||
err = pdfWriter.AttachFile(emFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return pdfWriter.WriteToFile(outputPath) | ||
} | ||
|
||
func addAttachmentFromContent(inputPath string, attachment string, outputPath string) error { | ||
// Read the input pdf file. | ||
f, err := os.Open(inputPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
pdfReader, err := model.NewPdfReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pdfWriter, err := pdfReader.ToWriter(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get attachment file content. | ||
content, err := os.ReadFile(attachment) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := 0; i < 2; i++ { | ||
// Instantiate embedded file object from existing file content. | ||
eFile, err := model.NewEmbeddedFileFromContent(content) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
eFile.Name = fmt.Sprintf("dummy%d.xml", i+1) | ||
eFile.Description = fmt.Sprintf("Sample Attachment %d", i+1) | ||
eFile.FileType = mimetype.Detect(content).String() | ||
eFile.Relationship = model.RelationshipData | ||
|
||
err = pdfWriter.AttachFile(eFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return pdfWriter.WriteToFile(outputPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Retrieve list of attachment file and save it locally. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/unidoc/unipdf/v3/common/license" | ||
"github.com/unidoc/unipdf/v3/model" | ||
) | ||
|
||
func init() { | ||
// Make sure to load your metered License API key prior to using the library. | ||
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io | ||
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func main() { | ||
inputPath := "output.pdf" | ||
|
||
err := listAttachments(inputPath) | ||
if err != nil { | ||
fmt.Printf("%v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Done") | ||
} | ||
|
||
func listAttachments(inputPath string) error { | ||
// Read the input pdf file. | ||
f, err := os.Open(inputPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
pdfReader, err := model.NewPdfReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
files, err := pdfReader.GetAttachedFiles() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = os.Stat("output") | ||
if os.IsNotExist(err) { | ||
err = os.Mkdir("output", 0777) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, v := range files { | ||
err := os.WriteFile(filepath.Join("output", fmt.Sprintf("%s.xml", v.Name)), v.Content, 0655) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Name: %s; Hash: %s\n", v.Name, v.Hash) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.