Weasyprint on MacOS M1: cannot use unpack() on <cdata 'char *' NULL>
Question:
I’ve been trying to save some HTML into PDF using Weasyprint, but I get the error message cannot use unpack() on <cdata 'char *' NULL>
I believe this is a MacOS M1-related issue, given that the same code has been tested on a Windows computer and it seemed fine.
Here is a minimum setup of the code:
import weasyprint
def createpdf():
html = weasyprint.HTML(filename=f"./test.html", encoding='UTF-8')
html.write_pdf(f"./test.pdf")
if __name__ == '__main__':
createpdf()
And the HTML I am using is:
<html lang="en-US">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0;" />
</head>
<body>
<div>
<p>Test</p>
</div>
</body>
</html>
Setup
I am running a conda-based virtual environment (Python 3.9) and have
- Installed Weasyprint from their website
- Added weasyprint in my requirements.txt file (which has been installed)
- Manually installed missing dependencies to the conda environment with
$ conda install -c conda-forge fontconfig pango harfbuzz
Debugging attempts
I have attempted to use PyCharm’s debugger to look into the code step by step and have found that:
- The HTML object is being created successfully
- The path is detected correctly
I could not, however, understand why it fails. Could anybody help?
Answers:
Though I could not figure out exactly where the issue lay, I managed to solve it by reinstalling Weasyprint and its dependencies on my virtual environment.
- Installed Weasyprint from https://doc.courtbouillon.org/weasyprint/stable/
- In my conda environment, I then ran:
$ brew install python pango libffi
$ pip install weasyprint
$ conda install -c conda-forge fontconfig pango harfbuzz gtk2
I’ve been trying to save some HTML into PDF using Weasyprint, but I get the error message cannot use unpack() on <cdata 'char *' NULL>
I believe this is a MacOS M1-related issue, given that the same code has been tested on a Windows computer and it seemed fine.
Here is a minimum setup of the code:
import weasyprint
def createpdf():
html = weasyprint.HTML(filename=f"./test.html", encoding='UTF-8')
html.write_pdf(f"./test.pdf")
if __name__ == '__main__':
createpdf()
And the HTML I am using is:
<html lang="en-US">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0;" />
</head>
<body>
<div>
<p>Test</p>
</div>
</body>
</html>
Setup
I am running a conda-based virtual environment (Python 3.9) and have
- Installed Weasyprint from their website
- Added weasyprint in my requirements.txt file (which has been installed)
- Manually installed missing dependencies to the conda environment with
$ conda install -c conda-forge fontconfig pango harfbuzz
Debugging attempts
I have attempted to use PyCharm’s debugger to look into the code step by step and have found that:
- The HTML object is being created successfully
- The path is detected correctly
I could not, however, understand why it fails. Could anybody help?
Though I could not figure out exactly where the issue lay, I managed to solve it by reinstalling Weasyprint and its dependencies on my virtual environment.
- Installed Weasyprint from https://doc.courtbouillon.org/weasyprint/stable/
- In my conda environment, I then ran:
$ brew install python pango libffi
$ pip install weasyprint
$ conda install -c conda-forge fontconfig pango harfbuzz gtk2