Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Generating PDF through an html template #57

Open
sumit-popclub opened this issue Feb 28, 2024 · 2 comments
Open

Generating PDF through an html template #57

sumit-popclub opened this issue Feb 28, 2024 · 2 comments

Comments

@sumit-popclub
Copy link

I have a html template in which I will render data on the runtime and want to generate a pdf out of it. Is there any way I can do that using go fpdf.

this is my code

func usingGoFPDF() {
	now := time.Now()

	tmpl, err := template.ParseFiles(templateFileName)
	if err != nil {
		log.Fatalf("could not parse template: %v", err)
	}

	fmt.Println("template parsed", time.Since(now))

	var result bytes.Buffer

	data := AadharDetails{
		PhotoSrc:      "photo.jpg",
		FormId:        "1234567890",
		AadhaarNumber: "1234 5678 9012",
		FirstName:     "John",
		FatherName:    "Doe",
		DOB:           "01-01-2000",
		ContactPerson: "John Doe",
		Gender:        "Male",
		MobileNumber:  "1234567890",
		EmailId:       "[email protected]",
		Address:       "123, XYZ Street, ABC City, DEF State, 123456",
		HouseNo:       "123",
		Landmark:      "Near ABC",
		City:          "ABC",
		Street:        "XYZ",
		PostOffice:    "DEF",
		District:      "GHI",
		SubDistrict:   "JKL",
		State:         "MNO",
		Pincode:       "123456",
		Country:       "India",
	}

	fmt.Println("data set", time.Since(now))

	err = tmpl.Execute(&result, data)
	if err != nil {
		log.Fatalf("could not execute template: %v", err)
	}

	fmt.Println("template executed", time.Since(now))

	htmlData := result.String()

	// Create a PDF document
	pdf := fpdf.New("P", "mm", "A4", "")

	// Add a new page
	pdf.AddPage()

	fmt.Println(htmlData)
	pdf.Write(1.2, htmlData)

	err = pdf.OutputFileAndClose("output.pdf")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Time taken:", time.Since(now))
	fmt.Println("PDF generated successfully:", "output.pdf")

}

It's breaking with the error - panic: runtime error: index out of range [60] with length 0

@sbinet
Copy link
Contributor

sbinet commented Mar 21, 2024

apologies for the belated answer.

do you have an as simple as possible reproducer ? (so I could play a bit with that ?)

@PylotLight
Copy link

apologies for the belated answer.

do you have an as simple as possible reproducer ? (so I could play a bit with that ?)

Any html page will do.. I am also searching for a weasyprint(python) replacement in Go.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants