Skip to content

Commit 68e020a

Browse files
authored
Merge pull request #3 from circa10a/convert_images
#minor: support converting images to pdfs
2 parents dd19df5 + 0d3ae62 commit 68e020a

File tree

5 files changed

+84
-37
lines changed

5 files changed

+84
-37
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ HOSTNAME=github.com
33
NAMESPACE=circa10a
44
NAME=mailform
55
BINARY=terraform-provider-${NAME}
6-
VERSION=0.2.0
6+
VERSION=0.4.0
77
OS_ARCH=darwin_amd64
88

99
default: install

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,3 @@ If you wish to work on the provider, you'll first need [Go](http://www.golang.or
103103
To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
104104

105105
To generate or update documentation, run `go generate`.
106-
107-
In order to run the full suite of Acceptance tests, run `make testacc`.
108-
109-
*Note:* Acceptance tests create real resources, and often cost money to run.
110-
111-
```sh
112-
make testacc
113-
```

docs/resources/pdf.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ Render a PDF and write to a local file.
1717

1818
### Required
1919

20-
- `content` (String) Content of PDF
2120
- `filename` (String) The path to the PDF file that will be created
21+
22+
### Optional
23+
24+
- `content` (String) Content of PDF
2225
- `header` (String) Header/title of PDF
26+
- `image_filename` (String) The image file to be converted to a PDF. Typically used for postcards
2327

2428
### Read-Only
2529

examples/resources/mailform_pdf/pdf.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ resource "mailform_pdf" "example" {
1111
content = "Some resume contents"
1212
filename = "./test.pdf"
1313
}
14+
15+
resource "mailform_pdf" "converted_image" {
16+
image_filename = "./myimage.jpg"
17+
filename = "./myimage.pdf"
18+
}

internal/provider/resource_pdf.go

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,39 @@ func resourcePDF() *schema.Resource {
2323
DeleteContext: resourcePDFDelete,
2424

2525
Schema: map[string]*schema.Schema{
26+
"filename": {
27+
Description: "The path to the PDF file that will be created",
28+
Type: schema.TypeString,
29+
Required: true,
30+
ForceNew: true,
31+
},
2632
"header": {
2733
Description: "Header/title of PDF",
2834
Type: schema.TypeString,
29-
Required: true,
35+
Optional: true,
3036
ForceNew: true,
37+
ConflictsWith: []string{
38+
"image_filename",
39+
},
3140
},
3241
"content": {
3342
Description: "Content of PDF",
3443
Type: schema.TypeString,
35-
Required: true,
44+
Optional: true,
3645
ForceNew: true,
46+
ConflictsWith: []string{
47+
"image_filename",
48+
},
3749
},
38-
"filename": {
39-
Description: "The path to the PDF file that will be created",
50+
"image_filename": {
51+
Description: "The image file to be converted to a PDF. Typically used for postcards",
4052
Type: schema.TypeString,
41-
Required: true,
53+
Optional: true,
4254
ForceNew: true,
55+
ConflictsWith: []string{
56+
"header",
57+
"content",
58+
},
4359
},
4460
},
4561
}
@@ -48,30 +64,25 @@ func resourcePDF() *schema.Resource {
4864
func resourcePDFCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
4965
var diags diag.Diagnostics
5066

51-
pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", gofpdf.PageSizeLetter, "")
52-
53-
header := d.Get("header").(string)
54-
content := d.Get("content").(string)
67+
// Used for generating pdfs and also converting pdf's to images
5568
filename := d.Get("filename").(string)
5669

57-
pdf.AddPage()
58-
pdf.SetTitle(d.Get("header").(string), false)
59-
pdf.SetFont("Arial", "B", 16)
60-
// Calculate width of title and position
61-
wd := pdf.GetStringWidth(header) + 6
62-
pdf.SetX((210 - wd) / 2)
63-
// Title
64-
pdf.CellFormat(wd, 9, header, "", 1, "C", false, 0, "")
65-
// Line break
66-
pdf.Ln(10)
67-
pdf.SetFont("Arial", "", 11)
68-
pdf.SetAutoPageBreak(true, 2.00)
69-
// Write ze content
70-
pdf.Write(8, content)
71-
72-
err := pdf.OutputFileAndClose(filename)
73-
if err != nil {
74-
return diag.FromErr(err)
70+
// If an image, convert to pdf
71+
if imageFilename, ok := d.GetOk("image_filename"); ok {
72+
err := convertImage(imageFilename.(string), filename)
73+
if err != nil {
74+
defer resourcePDFDelete(ctx, d, filename)
75+
return diag.FromErr(err)
76+
}
77+
} else {
78+
// Generate content if not image
79+
header := d.Get("header").(string)
80+
content := d.Get("content").(string)
81+
82+
err := renderPDF(header, content, filename)
83+
if err != nil {
84+
return diag.FromErr(err)
85+
}
7586
}
7687

7788
outputContent, err := ioutil.ReadFile(filename)
@@ -116,3 +127,38 @@ func resourcePDFDelete(ctx context.Context, d *schema.ResourceData, meta any) di
116127
os.Remove(d.Get("filename").(string))
117128
return nil
118129
}
130+
131+
// renderPDF converts header + content to a pdf and writes to an output file
132+
func renderPDF(header string, content string, outputFilePath string) error {
133+
pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", gofpdf.PageSizeLetter, "")
134+
pdf.AddPage()
135+
pdf.SetTitle(header, false)
136+
pdf.SetFont("Arial", "B", 16)
137+
// Calculate width of title and position
138+
wd := pdf.GetStringWidth(header) + 6
139+
pdf.SetX((210 - wd) / 2)
140+
// Title
141+
pdf.CellFormat(wd, 9, header, "", 1, "C", false, 0, "")
142+
// Line break
143+
pdf.Ln(10)
144+
pdf.SetFont("Arial", "", 11)
145+
pdf.SetAutoPageBreak(true, 2.00)
146+
// Write ze content
147+
pdf.Write(8, content)
148+
149+
return pdf.OutputFileAndClose(outputFilePath)
150+
}
151+
152+
// convertImage converts input image path to pdf file
153+
func convertImage(inputFilePath, outputFilePath string) error {
154+
pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", gofpdf.PageSizeLetter, "")
155+
pdf.AddPage()
156+
pdf.Image(inputFilePath, 0, 0, 240, 480, false, "", 0, "")
157+
158+
err := pdf.OutputFileAndClose(outputFilePath)
159+
if err != nil {
160+
return err
161+
}
162+
163+
return nil
164+
}

0 commit comments

Comments
 (0)