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

  1. Installed Weasyprint from their website
  2. Added weasyprint in my requirements.txt file (which has been installed)
  3. 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:

  1. The HTML object is being created successfully
  2. The path is detected correctly

I could not, however, understand why it fails. Could anybody help?

Asked By: user21042054

||

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.

  1. Installed Weasyprint from https://doc.courtbouillon.org/weasyprint/stable/
  2. In my conda environment, I then ran:
$ brew install python pango libffi
$ pip install weasyprint
$ conda install -c conda-forge fontconfig pango harfbuzz gtk2
Answered By: user21042054
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.