Skip to content

Commit

Permalink
Add link tags for pdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfedr committed Jul 18, 2024
1 parent 83a5126 commit 1622c99
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
==================
### Changed
### Added
* Support for links in PDFs
### Fixed


Expand Down
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,15 @@ ctx.addPage(400, 800)
ctx.fillText('Hello World 2', 50, 80)
```

It is possible to add hyperlinks use `.beginTag(uri)` and `.closeTag()`:

```js
ctx.beginTag("https://google.com")
ctx.font = '22px Helvetica'
ctx.fillText('Hello World', 50, 80)
ctx.closeTag()
```

See also:

* [Image#dataMode](#imagedatamode) for embedding JPEGs in PDFs
Expand Down
19 changes: 19 additions & 0 deletions examples/pdf-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs')
const Canvas = require('..')

const canvas = Canvas.createCanvas(400, 200, 'pdf')
const ctx = canvas.getContext('2d')

let y = 80
let x = 50

ctx.beginTag("https://google.com")
ctx.font = '22px Helvetica'
ctx.fillText('node-canvas pdf', x, y)
ctx.closeTag()

fs.writeFile('out.pdf', canvas.toBuffer(), function (err) {
if (err) throw err

console.log('created out.pdf')
})
30 changes: 30 additions & 0 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ Context2d::Initialize(Napi::Env& env, Napi::Object& exports) {
InstanceMethod<&Context2d::CreatePattern>("createPattern"),
InstanceMethod<&Context2d::CreateLinearGradient>("createLinearGradient"),
InstanceMethod<&Context2d::CreateRadialGradient>("createRadialGradient"),
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
InstanceMethod<&Context2d::BeginTag>("beginTag", napi_default_method),
InstanceMethod<&Context2d::CloseTag>("closeTag", napi_default_method),
#endif
InstanceAccessor<&Context2d::GetFormat>("pixelFormat"),
InstanceAccessor<&Context2d::GetPatternQuality, &Context2d::SetPatternQuality>("patternQuality"),
InstanceAccessor<&Context2d::GetImageSmoothingEnabled, &Context2d::SetImageSmoothingEnabled>("imageSmoothingEnabled"),
Expand Down Expand Up @@ -3354,3 +3358,29 @@ Context2d::Ellipse(const Napi::CallbackInfo& info) {
}
cairo_set_matrix(ctx, &save_matrix);
}

#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)

/*
* Open and close a link tag
*/

void
Context2d::BeginTag(const Napi::CallbackInfo& info) {
Napi::String strValue;
if (!info[0].ToString().UnwrapTo(&strValue)) return;

std::string str = strValue.Utf8Value();
std::string uri = "uri='" + str + "'";

cairo_t *ctx = context();
cairo_tag_begin(ctx, CAIRO_TAG_LINK, uri.c_str());
}

void
Context2d::CloseTag(const Napi::CallbackInfo& info) {
cairo_t *ctx = context();
cairo_tag_end(ctx, CAIRO_TAG_LINK);
}

#endif
4 changes: 4 additions & 0 deletions src/CanvasRenderingContext2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ class Context2d : public Napi::ObjectWrap<Context2d> {
void SetFont(const Napi::CallbackInfo& info, const Napi::Value& value);
void SetTextBaseline(const Napi::CallbackInfo& info, const Napi::Value& value);
void SetTextAlign(const Napi::CallbackInfo& info, const Napi::Value& value);
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
void BeginTag(const Napi::CallbackInfo& info);
void CloseTag(const Napi::CallbackInfo& info);
#endif
inline void setContext(cairo_t *ctx) { _context = ctx; }
inline cairo_t *context(){ return _context; }
inline Canvas *canvas(){ return _canvas; }
Expand Down

0 comments on commit 1622c99

Please sign in to comment.