I’m trying to render an HTML form using data from a JSON file with JavaScript, so that i can save a populated form as a pdf. When I open the index.html file directly from the filesystem (using the file:// protocol), It’s unable to fetch the JSON data required to populate the form. The Javascript has also not run.
I have a Playwright script that navigates to the local HTML file and attempts to render it.
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
current_directory = os.getcwd()
index_html_path = os.path.join(current_directory, 'temp', 'index.html')
index_html_url = f'file://{index_html_path}'
page.goto(index_html_url)
page.pdf(path='formoffline.pdf')
print('Saved formoffline.pdf')
browser.close()
I expected the JSON data to be loaded and processed without issues. Instead, the output is just the plain HTML without any of the Javascript or JSON loaded. This issue occurs when I use Playwright to open the HTML file. When i run a server using liveserver or node, it is properly working in the same directory. I’m not sure what is causing this issue or how to resolve it.
Akhil Babu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.