title: How to run Headless Chrome in Azure Cloud Service or Azure Functions?
body: <p>I am trying to use Headless Chrome to generate a PDF file from a complex HTML file (contains images, SVGs, etc.). I am able to use wkhtmltopdf.exe on Cloud Service (Windows) to generate simple PDF file, but I really need Chrome to produce PDFs as close as possible to the HTML + SVG + Image.</p>
  
  <p>I was hoping to be able to run Headless Chrome in Azure Cloud Service or Azure Functions, but I cannot get it to work. I suppose this is due to restrictions on GDI. I was able to run my code and Headless Chrome in the Azure Emulator on my own machine, but once it is deployed nothing works. </p>
  
  <p>Below is the code I am currently running in Azure Functions (for Windows). I am using Puppeteer to take a screenshot of example.com. If I can get this to work, I suppose that generating PDF will become easy.</p>
  
  <pre><code>const fs = require('fs');
  const path = require('path');
  const puppeteer = require('puppeteer');
  const os = require('os');
  
  module.exports = function (context, req) {
      function failureCallback(error) {
          context.log("--&gt; Failure = '" + error + "'");
      }
  
      const chromeDir = path.normalize(__dirname + "/../node_modules/puppeteer/.local-chromium/win64-508693/chrome-win32/chrome.exe");
      context.log("--&gt; Chrome Path = " + chromeDir);
  
      const dir = path.join(os.tmpdir(), '/screenshots');
  
      if (!fs.existsSync(dir)){
          fs.mkdirSync(dir);
      }
  
      const screenshotPath = path.join(dir, "example.png");
      context.log("--&gt; Path = " + screenshotPath);
  
      let browser, page;
      puppeteer.launch({ executablePath: chromeDir, headless: true, args: [ '--no-sandbox', '--single-process', '--disable-gpu' ] })
          .then(b =&gt; {
              context.log("----&gt; 1");
              browser = b;
              return browser.newPage();
          }, failureCallback)
          .then(p =&gt; {
              context.log("----&gt; 2");
              page = p;
              return p.goto('https://www.example.com');
          }, failureCallback)
          .then(response =&gt; {
              context.log("----&gt; 3");
              return page.screenshot({path: screenshotPath, fullPage: true});  
          }, failureCallback)
          .then(r =&gt; {
              browser.close();
  
              context.res = {
                  body: "Done!"
              };
  
              context.done();            
          }, failureCallback);
  };
  </code></pre>
  
  <p>Below is the log when trying to execute the script.</p>
  
  <pre><code>2017-12-18T04:32:05  Welcome, you are now connected to log-streaming service.
  2017-12-18T04:33:05  No new trace in the past 1 min(s).
  2017-12-18T04:33:11.400 Function started (Id=89b31468-8a5d-43cd-832f-b641216dffc0)
  2017-12-18T04:33:20.578 JavaScript HTTP trigger function processed a request.
  2017-12-18T04:33:20.578 --&gt; Chrome Path D:\home\site\wwwroot\node_modules\puppeteer\.local-chromium\win64-508693\chrome-win32\chrome.exe
  2017-12-18T04:33:20.578 --&gt; Path = D:\local\Temp\screenshots\example.png
  2017-12-18T04:33:20.965 --&gt; Failure = 'Error: spawn UNKNOWN'
  2017-12-18T04:33:20.965 ----&gt; 2
  </code></pre>
  
  <p>The error "Failure = 'Error: spawn UNKNOWN'" is not clear. I made sure that the path I am using is correct using Kudu and PowerShell.</p>
  
  <p>I am looking for a way to run Chrome on Azure Cloud Service and/or Azure Functions (for Windows - in order to use my existing App Service plan). Anybody has also attempted to run Headless Chrome in Azure? I am open to any ideas which would help me to get this script to work?</p>