{"id":16408951,"url":"https://github.com/adriancs2/html-pdf-edge","last_synced_at":"2025-03-23T06:30:56.950Z","repository":{"id":64207070,"uuid":"573247332","full_name":"adriancs2/HTML-PDF-Edge","owner":"adriancs2","description":"Convert HTML to PDF by Using Microsoft Edge in C#","archived":false,"fork":false,"pushed_at":"2023-06-08T02:18:52.000Z","size":906,"stargazers_count":10,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-18T18:15:36.098Z","etag":null,"topics":["edge","html","pdf"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adriancs2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2022-12-02T02:31:53.000Z","updated_at":"2024-08-16T09:03:05.000Z","dependencies_parsed_at":"2024-10-28T15:27:21.248Z","dependency_job_id":"6b754ca8-b4d6-42d0-9a22-8e5cf81da43d","html_url":"https://github.com/adriancs2/HTML-PDF-Edge","commit_stats":{"total_commits":20,"total_committers":1,"mean_commits":20.0,"dds":0.0,"last_synced_commit":"6f80f3ee8b901a496aeeee608917347501dc3bb7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriancs2%2FHTML-PDF-Edge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriancs2%2FHTML-PDF-Edge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriancs2%2FHTML-PDF-Edge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriancs2%2FHTML-PDF-Edge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adriancs2","download_url":"https://codeload.github.com/adriancs2/HTML-PDF-Edge/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245066496,"owners_count":20555402,"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","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":["edge","html","pdf"],"created_at":"2024-10-11T06:18:30.318Z","updated_at":"2025-03-23T06:30:55.969Z","avatar_url":"https://github.com/adriancs2.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Convert HTML to PDF by Using Microsoft Edge\n\nLive Demo: [http://html-pdf-edge.adriancs.com/](http://html-pdf-edge.adriancs.com/)\n\nNuget: [https://www.nuget.org/packages/Html-PDF-Edge](https://www.nuget.org/packages/Html-PDF-Edge)\n\nPM\u003e NuGet\\Install-Package Html-PDF-Edge\n\n## Usage\n\nInstall the Nuget Package, or download the source code. Extract and add the C# class file `html-pdf-edge.cs` into your project.\n\nAvailable methods:\n\n- `PDF.GeneratePdf(string urlHtml, string filePathPDF)`\n- `PDF.PublishUrl(string url, string filenamePdf, TransmitMethod transmitMethod)`\n- `PDF.PublishHtmlInline(string htmlContent)`\n- `PDF.PublishHtmlAttachment(string htmlContent, string filenamePdf)`\n- `PDF.PublishHtml(string htmlContent, string filenamePdf, TransmitMethod transmitMethod)`\n\n----\n## Example of Usage\n\n`PDF.GeneratePdf`\n----\n\n```cs\nstring url = \"https://www.adriancs.com/demo/invoice.html\";\nstring url = \"file:///C:/Users/Username/Documents/invoice.html\";\nstring filePathPDF = @\"C:\\file.pdf\";\nPDF.GeneratePdf(url, filePathPDF);\n```\n\n`PDF.PublishUrl`\n----\n\n```cs\npublic enum TransmitMethod\n{\n    None,        // save the pdf and do nothing\n    Attachment,  // transmit the pdf as attachment\n    Inline,      // transmit the pdf and show it in browser\n    Redirect     // redirect the browser to the url of the pdf\n}\n\nPDF.TransmitMethod transmitMethod = PDF.TransmitMethod.Inline;\nPDF.PublishUrl(url, \"file.pdf\", transmitMethod);\n```\n\n`PDF.PublishHtmlInline`\n----\n\n```cs\nstring html = \"\u003chtml\u003e\u003chead\u003e\u003c/head\u003e\u003cbody\u003e\u003ch1\u003eHello\u003c/h1\u003e\u003c/body\u003e\u003c/html\u003e\";\nPDF.PublishHtmlInline(html);\n```\n\n`PDF.PublishHtmlAttachment`\n----\n\n```cs\nstring html = \"\u003chtml\u003e\u003chead\u003e\u003c/head\u003e\u003cbody\u003e\u003ch1\u003eHello\u003c/h1\u003e\u003c/body\u003e\u003c/html\u003e\";\nPDF.PublishHtmlAttachment(html, \"file.pdf\");\n```\n\n`PDF.PublishHtml`\n----\n\n```cs\nPDF.TransmitMethod transmitMethod = PDF.TransmitMethod.Inline;\nPDF.PublishHtml(html, \"file.pdf\", transmitMethod);\n```\n\n## Important CSS Properties For Generating PDF\nThere are a few necessary CSS that you have to include in the HTML page in order for this to work properly.\n\n1. Set page margin to 0 (zero)\n2. Set paper size\n3. Wrap all content within a \"div\" with fixed width and margin\n4. Use CSS of page-break-always to split between pages.\n5. All fonts must already installed or hosted in your website\n6. URL links for images, external css stylesheet reference must include the root path.\n\n**1. Set page margin to 0 (zero)**\n```\n@page {\n    margin: 0;\n}\n```\nThe purpose of doing this is to hide the header and footer:\n\n![](https://raw.githubusercontent.com/adriancs2/HTML-PDF-Edge/main/wiki/screenshot4.png)\n\n**2. Set paper size**\n\nExample 1:\n```\n@page {\n    margin: 0;\n    size: A4 portrait;\n}\n```\nExample 2:\n```\n@page {\n    margin: 0;\n    size: letter landscape;\n}\n```\nExample 3: custom size (inch) *width then height\n```\n@page {\n    margin: 0;\n    size: 4in 6in;\n}\n```\nExample 4: custom size (cm) *width then height\n```\n@page {\n    margin: 0;\n    size: 14cm 14cm;\n}\n```\nFor more options/info on the CSS of @page, you may refer:\n\nhttps://developer.mozilla.org/en-US/docs/Web/CSS/@page/size\n\n**3. Wrap all content within a DIV with fixed width and margin**\n\nExample:\n```\n\u003cdiv class=\"page\"\u003e\n    \u003ch1\u003ePage 1\u003c/h1\u003e\n    \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n    \u003c!-- The rest of the body content --\u003e\n\u003c/div\u003e\n```\nStyle the \"div\" with class \"page\" (act as the main block/wrapper/container). Since the page has zero margin, we need to manually specified the top margin in CSS:\n```\nCSS\n.page {\n    width: 18cm;\n    margin: auto;\n    margin-top: 10mm;\n}\n```\nThe **width** has to be specified.\n\nThe \"**margin: auto**\" will align the div block at center horizontally.\n\n\"**margin-top: 10mm**\", will provide space between the main block and the edge of the paper at top section.\n\n**4. Use CSS of \"page-break-always\" to split between pages.**\n\nTo split pages, use a \"div\" and style with CSS of \"page-break-after\".\n```\npage-break-after: always\n```\nExample:\n```\n\u003cdiv class=\"page\"\u003e\n    \u003ch1\u003ePage 1\u003c/h1\u003e\n    \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n\u003c/div\u003e\n\n\u003cdiv style=\"page-break-after: always\"\u003e\u003c/div\u003e\n\n\u003cdiv class=\"page\"\u003e\n    \u003ch1\u003ePage 2\u003c/h1\u003e\n    \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n\u003c/div\u003e\n\n\u003cdiv style=\"page-break-after: always\"\u003e\u003c/div\u003e\n\n\u003cdiv class=\"page\"\u003e\n    \u003ch1\u003ePage 3\u003c/h1\u003e\n    \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n\u003c/div\u003e\n```\n**5. All fonts must already installed or hosted in your website**\n\nThe font rendering might not be working properly if the fonts are hosted at 3rd party's server, for example: Google Fonts. Try install the fonts into your server Windows OS or host the fonts within your website.\n\n**6. URL links for images, external css stylesheet reference must include the root path.**\n\nFor example, the following img tag might not be rendered properly. The image has the potential to be missing in the final rendered PDF output.\n\n```\n\u003cimg src=\"logo.png\" /\u003e\n\u003cimg src=\"images/logo.png\" /\u003e\n```\nIn stead, include the root path like this:\n```\n\u003cimg src=\"/logo.png\" /\u003e\n\u003cimg src=\"/images/logo.png\" /\u003e\n```\n\n## The sample of full HTML page:\n``` \n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003eDocument\u003c/title\u003e\n    \u003cstyle type=\"text/css\"\u003e\n        h1 {\n            margin: 0;\n            padding: 0;\n        }\n        .page {\n            margin: auto;\n            margin-top: 10mm;\n            border: 1px solid black;\n            width: 18cm;\n            height: 27cm;\n        }\n\n        @page {\n            margin: 0;\n            size: A4 portrait;\n        }\n    \u003c/style\u003e\n\u003c/head\u003e\n\n\u003cbody\u003e\n\n    \u003cdiv class=\"page\"\u003e\n        \u003ch1\u003ePage 1\u003c/h1\u003e\n        \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n    \u003c/div\u003e\n\n    \u003cdiv style=\"page-break-after: always\"\u003e\u003c/div\u003e\n\n    \u003cdiv class=\"page\"\u003e\n        \u003ch1\u003ePage 2\u003c/h1\u003e\n        \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n    \u003c/div\u003e\n\n    \u003cdiv style=\"page-break-after: always\"\u003e\u003c/div\u003e\n\n    \u003cdiv class=\"page\"\u003e\n        \u003ch1\u003ePage 3\u003c/h1\u003e\n        \u003cimg src=\"/pdf.jpg\" style=\"width: 100%; height: auto;\" /\u003e\n    \u003c/div\u003e\n\n\u003c/body\u003e\n\n\u003c/html\u003e\n```\n\n## How Does It Works Under the Hood\n\nMicrosoft Edge is a chromium based web browser which includes a built-in function that can generate PDF from HTML (or convert HTML to PDF).\n\nHere's the basic command line:\n\n```\nmsedge --headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf=\"{filePath}\" {url}\n```\n\nExample of command line with parameters:\n\n```\nmsedge --headless --disable-gpu --run-all-compositor-stages-before-draw\n--print-to-pdf=\"D:\\\\www.mysite.com\\temp\\pdf\\2059060194.pdf\"\nhttp://localhost:50964/temp/pdf/2059060194.html\n```\n\nThis project is basically gathered the values and uses a process to execute the command line:\n\n```cs\nusing System.Diagnostics;\n\nusing (var p = new Process())\n{\n    p.StartInfo.FileName = \"msedge\";\n    p.StartInfo.Arguments = $\"--headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf=\\\"{filePath}\\\" {url}\";\n    p.Start();\n    p.WaitForExit();\n}\n```\n\nAbove code will generate the PDF and save it at the location `filePath`, then it can be further transmitted for download.\n\nSince Google Chrome is also a Chromium based web browser, it can also be run with the same command line. You can check out my another PDF generator which is by using Google Chrome to convert HTML to PDF.\n\nHere: [https://github.com/adriancs2/HTML-PDF-Chrome](https://github.com/adriancs2/HTML-PDF-Chrome)\n\n## Running in Web Hosting Environment\n\nI have tested this implementation (using Edge) in the following environment:\n\n- Local IIS hosting\n- Web Hosting (smarterasp.net)\n- VPS Web Hosting\n\nAll above environment are able to generate PDF without issues. It runs smoothly without the need to configure the permission, Application Pool Identity and Website IIS authentication. \n\nThe following screenshot shows that the execution of MS Edge is allowed with default permission settings:\n\n![](https://raw.githubusercontent.com/adriancs2/HTML-PDF-Edge/main/wiki/screenshot2.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriancs2%2Fhtml-pdf-edge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadriancs2%2Fhtml-pdf-edge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriancs2%2Fhtml-pdf-edge/lists"}