WASHINGTON — Paul D. Ryan rode to power two years ago like a hero on a white horse, a reluctant candidate for House speaker elected to heal wounds left by his predecessor, who could not tame the incessant infighting between hard-line conservatives and establishment Republicans.

In one of his first real tests, Mr. Ryan discovered last week that those old wounds can reopen fast. But in President Trump, his mercurial partner in the White House, the speaker deftly found a foil to deflect some of the anger that had felled the man he succeeded, John A. Boehner.

President Trump’s fiscal deal with Democratic leaders in Congress — which passed the House with more than a third of Republicans voting against it — infuriated House conservatives, who struck first at Mr. Ryan, but ultimately turned their ire on the Trump White House. By week’s end, the men feeling the lash were Mr. Trump’s Treasury secretary and budget director. If anything, Mr. Ryan may have emerged stronger.

“It was thrown at him,” said Representative Mark Sanford, Republican of South Carolina and a member of the hard-line House Freedom Caucus, referring to the fiscal deal. “He didn’t create it; he’s reacting to it. I think he laid out a course that was acceptable to the conference as a whole, and to conservatives as well, and he had the rug pulled out from underneath him.”