Skip to content

Commit 3f2b940

Browse files
committed
Initial import
1 parent d98fde8 commit 3f2b940

23 files changed

+1715
-2
lines changed

LICENSE.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Licensing Information
2+
3+
This software package is a commercial product and requires a license
4+
code to operate.
5+
6+
The use of this software package is governed by the end-user license agreement
7+
(EULA) available at: https://unidoc.io/eula/
8+
9+
To obtain a Trial license code to evaluate the software, please visit
10+
https://unidoc.io/

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
# unihtml-examples
2-
Examples for UniHTML
1+
# UniHTML Examples
2+
3+
## Requirements
4+
5+
- Working UniHTML Server
6+
- UniHTML

directory/data/css/style.css

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
body {
2+
background-color: lightblue;
3+
}
4+
5+
img {
6+
max-width: 100%;
7+
}
8+
9+
.container {
10+
width: 90%;
11+
max-width: 600px;
12+
margin: 0 auto;
13+
/* border: 1px dotted #ccc; */
14+
}

directory/data/images/frog.jpg

2.3 MB
Loading

directory/data/index.html

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>My First Webpage</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<link rel="stylesheet" type="text/css" href="css/style.css">
7+
</head>
8+
<body>
9+
10+
<div class="container">
11+
12+
<h1>Heading 1</h1>
13+
<p>Vestibulum augue habitant tristique adipiscing fringilla hac sociis porttitor penatibus parturient euismod gravida penatibus penatibus ridiculus parturient class condimentum dapibus donec est morbi vivamus mi iaculis praesent vulputate. Leo consectetur arcu justo vitae dapibus dictumst augue at turpis mollis felis feugiat scelerisque ad nullam. Augue vestibulum per etiam maecenas praesent duis a per mi vestibulum.</p>
14+
<h2>Heading 2</h2>
15+
16+
<ul>
17+
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
18+
<li>Aliquam tincidunt mauris eu risus.</li>
19+
<li>Vestibulum auctor dapibus neque.</li>
20+
</ul>
21+
22+
<h2>Heading 2</h2>
23+
24+
<a href="https://en.wikipedia.org/wiki/File:Red_eyed_tree_frog_edit2.jpg">
25+
<img src="images/frog.jpg" alt="Red eyed tree frog">
26+
</a>
27+
<a href="https://en.wikipedia.org/wiki/File:Red_eyed_tree_frog_edit2.jpg">
28+
Link to image on Wikimedia Commons
29+
</a>
30+
31+
</div>
32+
</body>
33+
</html>

directory/directory.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This file is subject to the terms and conditions defined in
3+
* file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
package main
7+
8+
import (
9+
"fmt"
10+
"os"
11+
12+
"github.com/unidoc/unihtml"
13+
14+
"github.com/unidoc/unipdf/v3/creator"
15+
)
16+
17+
func main() {
18+
if len(os.Args) != 2 {
19+
fmt.Println("Err: provided invalid arguments. No UniHTML server path provided")
20+
os.Exit(1)
21+
}
22+
23+
// Establish connection with HTML Server.
24+
if err := unihtml.Connect(os.Args[1]); err != nil {
25+
fmt.Printf("Err: Connect failed: %v\n", err)
26+
os.Exit(1)
27+
}
28+
29+
// Create new PDF creator.
30+
c := creator.New()
31+
32+
// Create paragraph before HTML content.
33+
p := c.NewParagraph("Result is not calm in shangri-la, the enlightened mind, or chaos, but everywhere.")
34+
if err := c.Draw(p); err != nil {
35+
fmt.Printf("Err: Draw paragraph failed: %v\n", err)
36+
os.Exit(1)
37+
}
38+
39+
// Get the HTML document from provided directory.
40+
document, err := unihtml.NewDocument("data")
41+
if err != nil {
42+
fmt.Printf("Err: NewDocument failed: %v\n", err)
43+
os.Exit(1)
44+
}
45+
46+
// Trim last page content as we want to add new paragraph just after given unihtml document.
47+
document.TrimLastPageContent()
48+
49+
// Draw it in the creator context.
50+
if err = c.Draw(document); err != nil {
51+
fmt.Printf("Err: Draw failed: %v\n", err)
52+
os.Exit(1)
53+
}
54+
55+
// Create paragraph after the HTML document.
56+
paragraphAfter := c.NewParagraph("After scraping the lentils, brush escargot, margerine and coconut milk with it in an ice blender.")
57+
if err := c.Draw(paragraphAfter); err != nil {
58+
fmt.Printf("Err: DM")
59+
os.Exit(1)
60+
}
61+
62+
// Write the result into the file.
63+
if err = c.WriteToFile("directory.pdf"); err != nil {
64+
fmt.Printf("Err: %v\n", err)
65+
os.Exit(1)
66+
}
67+
}

directory/directory_chapter.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This file is subject to the terms and conditions defined in
3+
* file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
package main
7+
8+
import (
9+
"fmt"
10+
"os"
11+
12+
"github.com/unidoc/unihtml"
13+
14+
"github.com/unidoc/unipdf/v3/creator"
15+
)
16+
17+
func main() {
18+
if len(os.Args) != 2 {
19+
fmt.Println("Err: provided invalid arguments. No UniHTML server path provided")
20+
os.Exit(1)
21+
}
22+
23+
// Establish connection with the UniHTML Server.
24+
if err := unihtml.Connect(os.Args[1]); err != nil {
25+
fmt.Printf("Err: Connect failed: %v\n", err)
26+
os.Exit(1)
27+
}
28+
29+
// Get new PDF creator.
30+
c := creator.New()
31+
32+
// Create new chapter in the creator.
33+
ch := c.NewChapter("Directory")
34+
35+
// Read the content of the directory with HTML, CSS and Images files.
36+
htmlDocument, err := unihtml.NewDocument("data")
37+
if err != nil {
38+
fmt.Printf("Err: NewDocument failed: %v\n", err)
39+
os.Exit(1)
40+
}
41+
42+
// Add this document to the context of the chapter.
43+
if err = ch.Add(htmlDocument); err != nil {
44+
fmt.Printf("Err: Adding HTML Document failed: %v\n", err)
45+
os.Exit(1)
46+
}
47+
48+
// Draw the chapter in the context of the creator.
49+
if err = c.Draw(ch); err != nil {
50+
fmt.Printf("Err: Draw failed: %v\n", err)
51+
os.Exit(1)
52+
}
53+
54+
// Write the results to the file.
55+
if err = c.WriteToFile("directory_chapter.pdf"); err != nil {
56+
fmt.Printf("Err: %v\n", err)
57+
os.Exit(1)
58+
}
59+
}

directory/directory_pages.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is subject to the terms and conditions defined in
3+
* file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
package main
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"os"
12+
13+
"github.com/unidoc/unihtml"
14+
"github.com/unidoc/unihtml/sizes"
15+
16+
"github.com/unidoc/unipdf/v3/creator"
17+
)
18+
19+
func main() {
20+
if len(os.Args) != 2 {
21+
fmt.Println("Err: provided invalid arguments. No UniHTML server path provided")
22+
os.Exit(1)
23+
}
24+
25+
// Connect with the UniHTML Server.
26+
if err := unihtml.Connect(os.Args[1]); err != nil {
27+
fmt.Printf("Err: Connect failed: %v\n", err)
28+
os.Exit(1)
29+
}
30+
31+
// Connect with the UniHTML Server.
32+
c := creator.New()
33+
34+
// Create new document based on the directory containing HTML, CSS and Images.
35+
htmlDocument, err := unihtml.NewDocument("data")
36+
if err != nil {
37+
fmt.Printf("Err: NewDocument failed: %v\n", err)
38+
os.Exit(1)
39+
}
40+
41+
// Set Page size for the Document.
42+
if err = htmlDocument.SetPageSize(sizes.A5); err != nil {
43+
fmt.Printf("Err: Set page size for the document failed: %v\n", err)
44+
os.Exit(1)
45+
}
46+
47+
// Set Landscape orientation.
48+
htmlDocument.SetLandscapeOrientation()
49+
50+
// Extract pages directly from the HTML document.
51+
pages, err := htmlDocument.GetPdfPages(context.Background())
52+
if err != nil {
53+
fmt.Printf("Err: Getting Pages failed: %v\n", err)
54+
os.Exit(1)
55+
}
56+
57+
// Add pages one by one to the creator context.
58+
for _, page := range pages {
59+
if err = c.AddPage(page); err != nil {
60+
fmt.Printf("Err: Adding page failed: %v\n", err)
61+
os.Exit(1)
62+
}
63+
}
64+
65+
// Write creator result to the file 'directory_pages.pdf'.
66+
if err = c.WriteToFile("directory_pages.pdf"); err != nil {
67+
fmt.Printf("Err: %v\n", err)
68+
os.Exit(1)
69+
}
70+
}

doc.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* This file is subject to the terms and conditions defined in
3+
* file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
// Package examples contains different usage of the UniHTML. Each directory contains files with different input cases.
7+
package examples

go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/unidoc/unihtml-src/examples
2+
3+
go 1.15
4+
5+
require (
6+
github.com/sirupsen/logrus v1.9.0 // indirect
7+
github.com/stretchr/testify v1.8.1 // indirect
8+
github.com/unidoc/unihtml v0.7.0
9+
github.com/unidoc/unipdf/v3 v3.43.0
10+
github.com/unidoc/unitype v0.4.0 // indirect
11+
golang.org/x/crypto v0.6.0 // indirect
12+
golang.org/x/image v0.4.0 // indirect
13+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
14+
)

0 commit comments

Comments
 (0)