問題描述:
在使用img2pdf轉換png到pdf時候,報了如下錯誤
img2pdf.AlphaChannelError: Refusing to work on images with alpha channel
解決方案:
通過一番查找,發現是pngAlpha通道的問題,
不少解決方案都是自己寫的函數去除這個通道,但嘗試了,行不通。
最后找到個工具--Pngcrush,然后完美解決了這個問題。
來到官網
https://pmt.sourceforge.io/pngcrush/
下載二進制壓縮文件
本地解壓后,上傳服務器(Ubuntu)
文件目錄如下,是C語言的程序,后續需要構建編譯
編譯:
make -j8
注:-j8是指多線程執行,提高效率
編譯后的結果:生成pngcrush文件
執行pngcrush:
./pngcrush
將pngcrush設置環境變量,待后續去除png的Alpha通道用:
export PATH=$PATH:/root/wwww/selenium_screenshot/pngcrush-1.8.11/pcr010811/
去除png圖片(1234.png)的Alpha通道,重命名為4321.png:
pngcrush 1234.png 4321.png
然后再重新調用img2pdf,對4321.png進行轉換,轉換成outputl.pdf文件
大功告成! !
參考鏈接:
https://blog.csdn.net/qq_43668007/article/details/87197774
https://pmt.sourceforge.io/pngcrush
https://mlog.club/article/4090462