phantomjs-node

mangojang·2022년 4월 21일
0

Phantomjs-node

html 페이지를 pdf파일로 뽑아내는 node전용 라이브러리.

사용법

기본형태는 아래와 같음. 라이브러리 공식 github examples > render.js 를 참고 함.

page.render 를 실행하면 입력된 경로 대로 pdf가 저장됨.


	try{
        const instance = await phantom.create();
        const page = await instance.createPage();

        await page.property('viewportSize', { width: 1024, height: 600 });
        const status = await page.open('url 입력'); // pdf로 뽑고자 하는 html페이지 url 입력
        console.log(`Page opened with status [${status}].`);

        await page.render(`test.pdf`);// pdf경로+이름 입력 (기본경로 = 절대경로)
        console.log(`File created at [test.pdf]`);

        await instance.exit();

       
  }catch(error){
      console.log(error)
  }

주의할 점

필자는 처음 사용했을때, pdf결과물이 css가 다 깨져 나와서 적잖이 당황했음.

구글링을 통해 알아낸 결과, 비교적 최신 css를 표현하지 못 한다는 것을 알아냄.

grid layout, flex layout 등은 phantom.js 가 제대로 표현하지 못함. (필자는 flex layout 에서 문제가 있었고, 이에 관한 전반적인 수정으로 좀 고생함.ㅠ)

또한, png이미지도 인식하지 못함. (아무것도 안 나옴. img태그로 넣었을경우는 테두리만 나타남.)

이미지는 jpg, jpeg, svg를 사용해야한다.

참고문헌

https://gofogo.tistory.com/24

https://github.com/amir20/phantomjs-node

https://stackoverflow.com/questions/48703553/phantomjs-does-not-load-the-css-js-correctly

profile
한 걸음 한 걸음 계속 걷는 자가 일류다

0개의 댓글