Guides
Every UniHTML program has the same three steps. Connect to the server, build a
unihtml.Document from some HTML, then turn that document into PDF. The guides are
grouped by those steps, because the interesting decisions are in the first and the
last.
if err := unihtml.Connect("localhost:8080"); err != nil {
return err
}
doc, err := unihtml.NewDocument("report.html")
if err != nil {
return err
}
if err := doc.WriteToFile("report.pdf"); err != nil {
return err
}The middle step, page size and margins and waiting for the page to finish rendering, is where most surprises live. It is also where the four individual margin setters silently do nothing, because a document starts in relative positioning and that mode overrides all four.
New to UniHTML? The getting started guide covers getting a license key and running the server, which the client needs before any of this works.
Where to look
| Section | Covers |
|---|---|
| Input sources | The four kinds of input: an HTML file, a directory of HTML plus its assets, and a live web URL through NewDocument, or an inline string through NewDocumentFromString. |
| Page setup | Page size, orientation, margins, and waiting for JavaScript to finish before the page is captured. |
| PDF output | The three ways to get pages out, what each one keeps, and how to mix HTML with content built by the UniPDF creator. |
Pick the output path first
Settle this before you write any layout code, because the three paths do not behave the same and changing your mind later means rewriting the surrounding program.
Document.WriteToFile | GetPdfPages + c.AddPage | c.Draw(document) | |
|---|---|---|---|
| Page size and margins you set | Applied | Applied | Ignored |
| Clickable links | Kept | Kept | Dropped |
| Other creator content in the file | No | Yes, whole pages | Yes, in the text flow |
WriteToFile is the whole program when the PDF is nothing but the converted HTML.
GetPdfPages gives you the rendered pages to add to a document you are assembling
yourself, and is the path to use when the HTML contains links. c.Draw places the
rendered output inline like any other creator component, which is what you want
when a paragraph or a table has to follow the HTML on the same page, and which
costs you annotations and page control.
PDF output has a page for each, with the measurements behind that table.
Margins and units
Setting a single margin does nothing on its own. SetMarginLeft and its three
siblings leave the document in relative positioning, and in that mode all four
margins are forced to 1mm. Call SetMargins or SetPageSize first, then adjust
individual sides. Margins has the measured numbers.
Fractional lengths round to whole units before they reach the server, so
sizes.Inch(0.5) arrives as zero inches. Use millimeters or points for anything
smaller than a whole inch.