Render document in night mode

Q:How can I render a document in night mode?

A: Night mode is not supported yet in PSPDFKit for Web, but you could emulate it using PSPDFKit.Configuration#renderPageCallback. However, mapping inverse colors with precision is a recourse-intensive task. In the following example we choose to balance between color fidelity and performance by letting the browser invert every pixel color, so not only white gets rendered as black, but all the other colors get inverted too (blue as yellow, cyan as magenta, etc). You can consider how important color fidelity is for your use case before making use of it:

Copy
1
2
3
4
5
6
7
8
PSPDFKit.load({ 
  ...defaultConfiguration,
  renderPageCallback(ctx, pageIndex, pageSize) {
    ctx.globalCompositeOperation = "difference";
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, pageSize.width, pageSize.height);
  }
});

In order to achieve color fidelity when inverting colors for night mode, you could convert every pixel to HSV, invert the luminance value and then convert the pixel back to RGB to be rendered. In our tests, such approach results in a big performance hit, so we do not recommend it.

This has been tested with PSPDFKit for Web 2019.4.1