{"id":19764309,"url":"https://github.com/grahms/html2pdf","last_synced_at":"2025-07-29T06:32:54.757Z","repository":{"id":242017968,"uuid":"808473734","full_name":"GraHms/html2pdf","owner":"GraHms","description":"HTML 2 PDF is a Go library that provides an interface for converting HTML content to PDF using Chromium. It interacts with a headless Chromium instance and generate PDFs from HTML content.","archived":false,"fork":false,"pushed_at":"2024-05-31T06:35:28.000Z","size":5,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-27T04:38:15.212Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GraHms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-31T06:32:26.000Z","updated_at":"2024-08-11T02:21:41.000Z","dependencies_parsed_at":"2024-05-31T07:21:12.906Z","dependency_job_id":"bf2f10bf-324c-41e7-a155-9316b92625a8","html_url":"https://github.com/GraHms/html2pdf","commit_stats":null,"previous_names":["grahms/html2pdf"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/GraHms/html2pdf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fhtml2pdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fhtml2pdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fhtml2pdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fhtml2pdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GraHms","download_url":"https://codeload.github.com/GraHms/html2pdf/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fhtml2pdf/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267639569,"owners_count":24119780,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T04:13:23.459Z","updated_at":"2025-07-29T06:32:54.721Z","avatar_url":"https://github.com/GraHms.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# html2pdf\n\n`html2pdf` is a Go library that provides an interface for converting HTML content to PDF using Chromium. It interacts with a headless Chromium instance and generate PDFs from HTML content.\n\n## Installation\n\nTo install the `html2pdf` package, use `go get`:\n\n```sh\ngo get github.com/grahms/html2pdf\n```\n\n## Prerequisites\n\nEnsure that Chromium or Google Chrome is installed on your machine. You can download it from [here](https://www.chromium.org/getting-involved/download-chromium) or install it using a package manager:\n\n- On macOS:\n  ```sh\n  brew install chromium\n  ```\n- On Ubuntu/Debian:\n  ```sh\n  sudo apt-get install chromium-browser\n  ```\n\n## Usage\n\nHere's a basic example of how to use the `html2pdf` library to convert HTML content to a PDF file:\n\n### Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/grahms/html2pdf\"\n\t\"log\"\n\t\"os\"\n)\n\nfunc main() {\n\thtml := `\u003chtml\u003e\n\u003cbody\u003e\n\u003cdiv\u003etext\u003c/div\u003e\n\u003cimg src=\"https://pkg.go.dev/static/shared/gopher/package-search-700x300.jpeg\"/\u003e\n\u003cimg src=\"https://go.dev/images/gophers/motorcycle.svg\"/\u003e\n\u003cimg src=\"https://go.dev/images/go_google_case_study_carousel.png\" /\u003e\n\u003c/body\u003e\n\u003c/html\u003e`\n\n\t// Create a new PDFGenerator instance\n\tpdfGen, err := html2pdf.New()\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to initialize PDF generator: %v\", err)\n\t}\n\tdefer pdfGen.Close()\n\n\t// Convert HTML to PDF\n\tpdfBuffer, err := pdfGen.GeneratePDF(\u0026html)\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to convert HTML to PDF: %v\", err)\n\t}\n\n\t// Save the PDF buffer to a file\n\tif err := saveToFile(\"output.pdf\", pdfBuffer); err != nil {\n\t\tlog.Fatalf(\"Failed to save PDF to file: %v\", err)\n\t}\n\n\tfmt.Println(\"PDF successfully created: output.pdf\")\n}\n\n// saveToFile writes the PDF buffer to a file.\nfunc saveToFile(fileName string, data []byte) error {\n\treturn os.WriteFile(fileName, data, 0644)\n}\n```\n\n## Features\n\n- **Easy to use**: Simple API to generate PDFs from HTML content.\n- **Graceful shutdown**: Handles interrupt signals to ensure the Chromium instance is properly terminated.\n- **Mockable**: Provides an interface for easy testing and mocking.\n\n## API\n\n### `PDFGeneratorInterface`\n\n```go\ntype PDFGeneratorInterface interface {\n\tGeneratePDF(html *string) ([]byte, error)\n\tClose()\n}\n```\n\n### `PDFGenerator`\n\n#### `New() (*PDFGenerator, error)`\n\nCreates a new `PDFGenerator` and initializes the browser context.\n\n#### `GeneratePDF(html *string) ([]byte, error)`\n\nConverts HTML content to a PDF buffer.\n\n#### `Close()`\n\nShuts down the browser context.\n\n\n\n## Handling Interrupt Signals\n\nThe library automatically sets up a signal handler to gracefully shut down the `PDFGenerator` on interrupt signals.\n\n## Contributing\n\nContributions are welcome! Please open an issue or submit a pull request on GitHub.\n\n## License\n\nThis project is licensed under the MIT License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrahms%2Fhtml2pdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrahms%2Fhtml2pdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrahms%2Fhtml2pdf/lists"}