package vgpdf
import "gonum.org/v1/plot/vg/vgpdf"
Package vgpdf implements the vg.Canvas interface
using gofpdf (github.com/phpdave11/gofpdf).
Example_embedFonts shows how one can embed (or not) fonts inside
a PDF plot.
Code:play
Example_multipage shows how one can create a PDF with multiple pages.
Code:play
Example (EmbedFonts)¶
package main
import (
"log"
"os"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgpdf"
)
func main() {
p := plot.New()
pts := plotter.XYs{{X: 0, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 0}, {X: 1, Y: 1}}
line, err := plotter.NewLine(pts)
if err != nil {
log.Fatalf("could not create line: %v", err)
}
p.Add(line)
p.X.Label.Text = "X axis"
p.Y.Label.Text = "Y axis"
c := vgpdf.New(100, 100)
// enable/disable embedding fonts
c.EmbedFonts(true)
p.Draw(draw.New(c))
f, err := os.Create("testdata/enable-embedded-fonts.pdf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = c.WriteTo(f)
if err != nil {
log.Fatalf("could not write canvas: %v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("could not save canvas: %v", err)
}
}
Example (Multipage)¶
package main
import (
"fmt"
"image/color"
"log"
"os"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgpdf"
)
func main() {
c := vgpdf.New(5*vg.Centimeter, 5*vg.Centimeter)
for i, col := range []color.RGBA{{B: 255, A: 255}, {R: 255, A: 255}} {
if i > 0 {
// Add a new page.
c.NextPage()
}
p := plot.New()
pts := plotter.XYs{{X: 0, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 0}, {X: 1, Y: 1}}
line, err := plotter.NewLine(pts)
if err != nil {
log.Fatalf("could not create line: %v", err)
}
line.Color = col
p.Add(line)
p.Title.Text = fmt.Sprintf("Plot %d", i+1)
p.X.Label.Text = "X axis"
p.Y.Label.Text = "Y axis"
// Write plot to page.
p.Draw(draw.New(c))
}
f, err := os.Create("testdata/multipage.pdf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = c.WriteTo(f)
if err != nil {
log.Fatalf("could not write canvas: %v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("could not save canvas: %v", err)
}
}
Index ¶
- Constants
- type Canvas
- func New(w, h vg.Length) *Canvas
- func (c *Canvas) DPI() float64
- func (c *Canvas) DrawImage(rect vg.Rectangle, img image.Image)
- func (c *Canvas) EmbedFonts(v bool) bool
- func (c *Canvas) Fill(p vg.Path)
- func (c *Canvas) FillString(fnt font.Face, pt vg.Point, str string)
- func (c *Canvas) NextPage()
- func (c *Canvas) Pop()
- func (c *Canvas) Push()
- func (c *Canvas) Rotate(r float64)
- func (c *Canvas) Scale(x float64, y float64)
- func (c *Canvas) SetColor(clr color.Color)
- func (c *Canvas) SetLineDash(dashes []vg.Length, offs vg.Length)
- func (c *Canvas) SetLineWidth(w vg.Length)
- func (c *Canvas) Size() (w, h vg.Length)
- func (c *Canvas) Stroke(p vg.Path)
- func (c *Canvas) Translate(pt vg.Point)
- func (c *Canvas) WriteTo(w io.Writer) (int64, error)
Examples ¶
Constants ¶
const DPI = 72
DPI is the nominal resolution of drawing in PDF.
Types ¶
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas implements the vg.Canvas interface, drawing to a PDF.
func New ¶
New creates a new PDF Canvas.
func (*Canvas) DPI ¶
func (*Canvas) DrawImage ¶
DrawImage implements the vg.Canvas.DrawImage method.
func (*Canvas) EmbedFonts ¶
EmbedFonts specifies whether the resulting PDF canvas should embed the fonts or not. EmbedFonts returns the previous value before modification.
func (*Canvas) Fill ¶
func (*Canvas) FillString ¶
func (*Canvas) NextPage ¶
func (c *Canvas) NextPage()
NextPage creates a new page in the final PDF document. The new page is the new current page. Modifications applied to the canvas will only be applied to that new page.
func (*Canvas) Pop ¶
func (c *Canvas) Pop()
func (*Canvas) Push ¶
func (c *Canvas) Push()
func (*Canvas) Rotate ¶
func (*Canvas) Scale ¶
func (*Canvas) SetColor ¶
func (*Canvas) SetLineDash ¶
func (*Canvas) SetLineWidth ¶
func (*Canvas) Size ¶
func (*Canvas) Stroke ¶
func (*Canvas) Translate ¶
func (*Canvas) WriteTo ¶
WriteTo writes the Canvas to an io.Writer. After calling Write, the canvas is closed and may no longer be used for drawing.
Source Files ¶
vgpdf.go
- Version
- v0.15.0 (latest)
- Published
- Oct 22, 2024
- Platform
- linux/amd64
- Imports
- 18 packages
- Last checked
- 1 week ago –
Tools for package owners.