Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kkashyap1707/b039a83d0d2b4eed61ba2c3dd52ffcf8 to your computer and use it in GitHub Desktop.

Select an option

Save kkashyap1707/b039a83d0d2b4eed61ba2c3dd52ffcf8 to your computer and use it in GitHub Desktop.

Revisions

  1. @svet-b svet-b revised this gist Sep 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion grafana_pdf_via_snapshot.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ## Get PDF export of Grafana dashboard

    ## Note
    **As currently described, this approach does not work. The reason for that is that creating a snapshot via the API does not actually populate any data, as described in e.g. https://community.grafana.com/t/snapshot-using-http-api-does-nothing/. An alternative approach which does work described in https://gist.github.com/svet-b/1ad0656cd3ce0e1a633e16eb20f66425.
    __As currently described, this approach does not work. The reason for that is that creating a snapshot via the API does not actually populate any data, as described in e.g. https://community.grafana.com/t/snapshot-using-http-api-does-nothing/. An alternative approach which does work described in https://gist.github.com/svet-b/1ad0656cd3ce0e1a633e16eb20f66425.__

    ### Prerequisites

  2. @svet-b svet-b revised this gist Sep 1, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions grafana_pdf_via_snapshot.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    ## Get PDF export of Grafana dashboard

    ## Note
    **As currently described, this approach does not work. The reason for that is that creating a snapshot via the API does not actually populate any data, as described in e.g. https://community.grafana.com/t/snapshot-using-http-api-does-nothing/. An alternative approach which does work described in https://gist.github.com/svet-b/1ad0656cd3ce0e1a633e16eb20f66425.

    ### Prerequisites

    Packages:
  3. @svet-b svet-b renamed this gist Sep 1, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @svet-b svet-b revised this gist Aug 28, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions pdf.js
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,10 @@ const outfile = process.argv[3];
    const page = await browser.newPage();

    await page.goto(url, {waitUntil: 'networkidle2'});

    // Wait for any warnings to disappear
    await page.waitFor(10000);

    // page.pdf() is currently supported only in headless mode.
    // @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118
    await page.pdf({
  5. @svet-b svet-b revised this gist Aug 28, 2018. 1 changed file with 14 additions and 3 deletions.
    17 changes: 14 additions & 3 deletions pdf.js
    Original file line number Diff line number Diff line change
    @@ -19,21 +19,32 @@
    const puppeteer = require('puppeteer');

    // URL to load should be passed as first parameter
    var url = process.argv[2];
    const url = process.argv[2].replace (/(^")|("$)/g, '');

    // Output file name should be second parameter
    var outfile = process.argv[3];
    const outfile = process.argv[3];

    // TODO: Output an error message if number of arguments is not right or arguments are invalid

    (async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto(url, {waitUntil: 'networkidle2'});
    // page.pdf() is currently supported only in headless mode.
    // @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118
    await page.pdf({
    path: outfile,
    format: 'letter'
    format: 'A4',
    scale: 0.75,
    displayHeaderFooter: false,
    printBackground: false,
    margin: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    },
    });

    await browser.close();
  6. @svet-b svet-b revised this gist Aug 28, 2018. 2 changed files with 67 additions and 2 deletions.
    29 changes: 27 additions & 2 deletions grafana_pdf.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,37 @@
    ## Get PDF export of Grafana dashboard

    Create Grafana API key from web UI (http://docs.grafana.org/http_api/auth/ has some outdated instructions but the idea holds). Save it in an environment variable, along with your Grafana instance URL
    ### Prerequisites

    Packages:
    - The `jq` JSON processing command line tool, which is available for most distributions (https://stedolan.github.io/jq/, `sudo apt install jq`)
    - NodeJS, and the `puppeteer` package (`npm install puppeteer`), which is used to run headless Chrome

    Scripts:
    - The `pdf.js` file attached here, which carries out the PDF conversion

    Also, create Grafana API key from web UI (http://docs.grafana.org/http_api/auth/ has some outdated instructions but the idea holds). Save it in an environment variable, along with your Grafana instance URL
    ```
    export APIKEY=eyJrIjoiMFdmMnA3YlRZTFF5bktqUmhRRk9nUzBISEo0dkZMNW0iLCJuIjoicGRmIiwiaWQiOjF9
    export GURL=http://localhost:3000
    ```
    Now we need to take a snapshot of the dashboard to be exported. We can do this via the API, per http://docs.grafana.org/http_api/snapshot/, but in order to request this we actually need the full dashboard JSON. This can be obtained via another API call, based on the dashboard ID - here saved as `dashboard.json`:

    ### Process
    First we need to take a snapshot of the dashboard to be exported. We can do this via the API, per http://docs.grafana.org/http_api/snapshot/, but in order to request this we actually need the full dashboard JSON. This can be obtained via another API call, based on the dashboard ID - here saved as `dashboard.json`:
    ```
    export DASHID=x3g4Wx5ik
    curl -X GET -H "Authorization: Bearer $APIKEY" $GURL/api/dashboards/uid/$DASHID -o dashboard.json
    ```

    Now create the snapshot via a call to the respective API endpoint. Note that here we directly submit the dashboard JSON of which we'd like a snapshot.
    ```
    curl -X POST -H "Authorization: Bearer $APIKEY" -H "Content-Type: application/json" -d @dashboard.json $GURL/api/snapshots -o snapshot.json
    ```
    The result is saved in `snapshot.json`. From this we can extract the URL of the generated snapshot.
    ```
    export SNAPURL=`jq .url snapshot.json`
    ```

    Now export to PDF with
    ```
    node pdf.js $SNAPURL output.pdf
    ```
    40 changes: 40 additions & 0 deletions pdf.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    /**
    * Copyright 2017 Google Inc. All rights reserved.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */

    'use strict';

    const puppeteer = require('puppeteer');

    // URL to load should be passed as first parameter
    var url = process.argv[2];
    // Output file name should be second parameter
    var outfile = process.argv[3];

    // TODO: Output an error message if number of arguments is not right or arguments are invalid

    (async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url, {waitUntil: 'networkidle2'});
    // page.pdf() is currently supported only in headless mode.
    // @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118
    await page.pdf({
    path: outfile,
    format: 'letter'
    });

    await browser.close();
    })();
  7. @svet-b svet-b created this gist Aug 28, 2018.
    12 changes: 12 additions & 0 deletions grafana_pdf.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    ## Get PDF export of Grafana dashboard

    Create Grafana API key from web UI (http://docs.grafana.org/http_api/auth/ has some outdated instructions but the idea holds). Save it in an environment variable, along with your Grafana instance URL
    ```
    export APIKEY=eyJrIjoiMFdmMnA3YlRZTFF5bktqUmhRRk9nUzBISEo0dkZMNW0iLCJuIjoicGRmIiwiaWQiOjF9
    export GURL=http://localhost:3000
    ```
    Now we need to take a snapshot of the dashboard to be exported. We can do this via the API, per http://docs.grafana.org/http_api/snapshot/, but in order to request this we actually need the full dashboard JSON. This can be obtained via another API call, based on the dashboard ID - here saved as `dashboard.json`:
    ```
    export DASHID=x3g4Wx5ik
    curl -X GET -H "Authorization: Bearer $APIKEY" $GURL/api/dashboards/uid/$DASHID -o dashboard.json
    ```