2017-02-26 5 views
0

jsreportphantom pdf recipe을 사용하여보기를 ASP.NET Core의 pdf로 렌더링합니다. 프로젝트는 Azure에서 App Service으로 실행됩니다. 내 로컬 컴퓨터에서 제대로 작동하지만, Azure에 게시하면 아래 오류 메시지가 표시됩니다.ASP.NET 코어 Azure의 Phantom pdf

Exception: Call to Node module failed with error: ReferenceError: e is not defined at module.exports (D:\home\site\wwwroot\serviceCallDetailsPdf.js:18:26)

여기 내 프로젝트 (wwwroot에, 뷰, 컨트롤러와 같은 디렉토리 수준 등)의 루트에 내 js 파일입니다.

module.exports = function (callback, html, htmlFooter) { 
    var jsreport = require('jsreport-core')(); 

    jsreport.init().then(function() { 
     return jsreport.render({ 
      template: { 
       content: html, 
       engine: 'jsrender', 
       recipe: 'phantom-pdf', 
       phantom: { 
        orientation: "landscape", 
        allowLocalFilesAccess: true, 
        footer: htmlFooter 
       } 
      } 
     }).then(function (resp) { 
      callback(/* error */ null, resp.content.toString()); 
     }); 
    }).catch(function (e) { 
     callback(/* error */ e, null); 
    }); 

    callback(/* error */ e, null); 
}; 

참고 : 마지막 콜백은 일반적으로 존재하지 않습니다. 제거하면 Node.js 모듈의 변경 내용이 60000 밀리 초 후에 시간 초과되고 물론 표시하려면 60 초가 걸립니다.

내가 그렇지 않으면 내가 Node.js 내가 부르고 파일을 미세 없습니다라고 오류가 발생하는 것입니다 publishOptions*.jsnode_modules을 포함하는 내 project.json 파일을 업데이트했다. 나는 이것이 적절한 방법이 아니라는 것을 안다. (내가 필요로하는 것만 복사하기 위해 Gulp을 사용해야하지만, 나는 그것을 먼저 작동시키고 싶다).

나는 ConfigureServices-services.AddNodeServices();project.json에도 "Microsoft.AspNetCore.NodeServices": "1.1.0"을 추가했습니다. 여기

자동으로 생성 된 내 package.json 파일입니다

{ 
    "name": "projectname", 
    "version": "1.0.0", 
    "description": "", 
    "main": "serviceCallDetailsPdf.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "jsreport-core":"1.1.1", 
    "jsreport-jsrender": "1.0.1", 
    "jsreport-phantom-pdf": "1.3.1" 
    } 
} 

여기, 마지막으로 내 Action :

[HttpGet] 
public async Task<IActionResult> ServiceCallDetailsToPdf(int id) 
{ 
    var call = _repository.PullCallDetails(id, User.Identity.RegionId(), User.Identity.TimeZoneOffsetId()); 

    // Render view as string 
    var html = await _viewRenderService.RenderToStringAsync("Render/ServiceCallDetailsPdf", call); 
    html = html.Replace("<style></style>", @"<style>@font-face{font-family:'Open Sans';src:url({#asset OpenSans-Regular.ttf @encoding=dataURI});format('ttf');}*{font-family:'Open Sans';}h2{font-weight:300;line-height:1.1;}</style>"); 

    var htmlFooter = "<style>*{font-family:'Open Sans';text-align:center;}</style>" + "Page {#pageNum} of {#numPages}"; 

    // Invoke node job to create the pdf 
    var result = await _nodeServices.InvokeAsync<byte[]>("./serviceCallDetailsPdf", html, htmlFooter); 

    // Content disposition 'inline' will return the pdf to the browser and not download it 
    var contentDisposition = new ContentDispositionHeaderValue("inline"); 
    // Add file name to content disposition so if it is downloaded, it uses the correct file name 
    contentDisposition.SetHttpFileName("dispatch" + id.ToString() + ".pdf"); 
    Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString(); 

    return File(result, "application/pdf"); 
} 

가 호출되는 javascript 파일을 디버깅 할 수 있습니까? 나는 이것이 작동하지 않는 이유를 알아 내려고 며칠 동안 많은 시간을 보냈습니다. 어떤 도움이라도 대단히 감사하겠습니다.


업데이트

나는이 링크에서 내 대답을 찾을 생각 :

https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox

PhantomJS as web job in Azure

이것은 기본적으로 당신이에서 팬텀 PDF 조리법을 사용할 수 없습니다 의미를 Azure App 서비스. 다른 사람이 확인할 수 있습니까? 이 경우 Azure App 서비스에서 작동하는 html to pdf 렌더러를 아는 사람이 있습니까?

+0

귀하는 이미 연결된 다른 질문을 통해 답변을 찾았습니다 (기본적으로이 질문의 사본입니다). 웹 응용 프로그램에서는 ActiveX/GDI/등을 사용할 수 없습니다. –

답변

1

기본 Windows 서버에서 실행되는 Azure App 서비스에서는 phantomjs 및 기타 pdf 렌더링이 작동하지 않습니다. 그러나 이제 jsreport가 잘 작동하는 Linux에서 Azure App 서비스를 사용할 수 있습니다.

새로운 App Service를 생성하고 노드 기반 컨테이너를 사용할 때 Linux에서 Web App로 전환하면됩니다. 그런 다음 docker hub를 사용하여 jsreport가있는 이미지를 Web App에 바로 다운로드 할 수 있습니다. 가장 간단한 방법은 공식 jsreport 이미지를 사용하는 것인데, 자신 만의 jsreport 이미지를 사용하는 것도 쉽습니다.

링크
Tutorial how run jsreport on Azure Web App on linux
jsreport official docker image

내가 실제로 asp.net 코어와 같은 응용 프로그램에서 실행하는 방법을 대답하지 않았다 알고 있어요. 나는 현재 어떻게해야할지 모르지만, asp.net 응용 프로그램과 jsreport를 별도의 응용 프로그램으로 실행하는 것이 좋습니다. REST를 통해 jsreport 렌더링을 쉽게 호출 할 수 있습니다. 또한 아키텍처 디자인을 향상시킬 수 있습니다.