Skip to content

Revit PDF export sample:Sample to demonstrate how to retrieve your 2d views from a Revit file in the PDF format.

License

Notifications You must be signed in to change notification settings

autodesk-platform-services/aps-export-revit-pdf-md

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Revit PDFs Exporter MD

Platforms node.js npm License

oAuth2 Data-Management Model-Derivative BIM360 ACC

This sample shows a way to export PDFs from a Revit designs using Model Derivative.

DEMO: aps-revit-pdf-exporter.autodesk.io/

Introduction

When you submit a translation for a Revit 2022 file or later, it generates the 2D views as PDFs (refer here). We can take advantage of this fact to download these PDFs wherever they're available (Bucket, ACC, BIM 360...). Many workflows are possible in this scenarion, integrating with Webhooks, 3rd party services, other products...

thumbnail

The approach

We are basically take advantage of the manifest in order to list and filter all the 2D views, and from there we can download the derivative using the Derivative Download endpoint, just like in the snippet below:

service.getDownloadUrls = async (version_id, token) => {
  const resp = await new APS.DerivativesApi().getManifest(
    version_id.replace("-", "/"),
    null,
    internalAuthClient,
    token
  );
  let derivatives = resp.body.derivatives[0].children;
  let pdfViews = derivatives.filter(
    (v) => v.role == "2d" && !!v.properties["Print Setting"]
  );
  let pdfDerivatives = pdfViews.map((v) =>
    v.children.find((d) => d.role == "pdf-page")
  );
  let downloadUrls = [];
  for (const derivative of pdfDerivatives) {
    let newDerivativeUrl = await getSignedUrlFromDerivative(
      version_id.replace("-", "_"),
      derivative,
      token
    );
    downloadUrls.push(newDerivativeUrl);
  }
  // return downloadUrls;
  return {
    derivatives: downloadUrls,
    RVTVersion:
      resp.body.derivatives[0].properties["Document Information"].RVTVersion,
  };
};

async function getSignedUrlFromDerivative(urn, derivative, token) {
  let url = `https://developer.api.autodesk.com/modelderivative/v2/designdata/${urn.replaceAll(
    "=",
    ""
  )}/manifest/${derivative.urn}/signedcookies?useCdn=true`;

  let options = {
    method: "GET",
    headers: {
      Authorization: "Bearer " + token.access_token,
    },
  };

  let resp = await fetch(url, options);
  let respJSON = await resp.json();
  let policy = resp.headers.raw()["set-cookie"][0].split("=")[1].split(";")[0];
  let keypair = resp.headers.raw()["set-cookie"][1].split("=")[1].split(";")[0];
  let signature = resp.headers
    .raw()
    ["set-cookie"][2].split("=")[1]
    .split(";")[0];
  let data = {
    name: derivative.urn.split("/").slice(-1)[0],
    url: respJSON.url,
    "CloudFront-Policy": policy,
    "CloudFront-Key-Pair-Id": keypair,
    "CloudFront-Signature": signature,
  };

  return data;
}

In this case, we're downloading the PDFs from BIM 360/ACC, so we just need a version id.

Development

Prerequisites

Setup & Run

  • Clone this repository
  • Install dependencies: yarn install or npm install
  • Setup environment variables:
    • APS_CLIENT_ID - your APS application client ID
    • APS_CLIENT_SECRET - your APS application client secret
    • APS_CALLBACK_URL - URL for your users to be redirected to after they successfully log in with their Autodesk account
      • For local development, the callback URL is http://localhost:8080/api/auth/callback
      • For applications deployed to a custom domain, the callback URL is http://<your-domain>/api/auth/callback or https://<your-domain>/api/auth/callback
      • Do not forget to update the callback URL for your application in https://forge.autodesk.com/myapps as well
    • SERVER_SESSION_SECRET - arbitrary phrase used to encrypt/decrypt server session cookies
  • Run the server: yarn start or npm start

When using Visual Studio Code, you can specify the env. variables listed above in a .env file in this folder, and run & debug the application directly from the editor.

Troubleshooting

For this workflow to work, your Revit file needs to be from version 2022 or later, and it needs to be published after November 4th of 2021. This last restriction is because only after this date the extractor started adding the Revit version in the manifest (refer to this blog).

Also, for it to download the PDFs you might need to change your browser settings (allow pop-ups and download PDFs instead of showing in new tab).

Refer here to configure your browser to download PDFs instead of opening them in a new tab: https://www.howtogeek.com/721441/how-to-download-pdfs-instead-of-previewing-them-in-chrome-firefox-and-edge/ chrome_setting

Refer here to configure your browser to allow pop-ups (each PDF we download opens a new pop-up): https://blog.getadblock.com/how-to-disable-pop-up-blockers-in-every-browser-a1cccbae53e7 chrome_setting

License

This sample is licensed under the terms of the MIT License. Please see the LICENSE file for full details.

Written by

João Martins @JooPaulodeOrne2, APS Partner Development

About

Revit PDF export sample:Sample to demonstrate how to retrieve your 2d views from a Revit file in the PDF format.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published