使用tcolorbox
。
\documentclass{article}
\usepackage[a6paper]{geometry}% ridiculously small page for illustration only
\usepackage{tcolorbox}
\tcbuselibrary{breakable,listings,skins}
\begin{document}
\tcbinputlisting{enhanced jigsaw,breakable,listing only,listing file=knuth.tex}
\end{document}