@@ -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 {
4864func 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